Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi coder i have an createuser page in which i have some field and submit button

When i click on my submit button it will save details into database

aspx.cs code for createuser page:

C#
protected void btnSubmit_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["WebGallery"].ToString()))
    {
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();

        string strSQL = "SELECT * FROM NewUser WHERE UserName = '" + tbName.Text + "'";

        da.SelectCommand = new SqlCommand(strSQL);
        da.SelectCommand.Connection = con;
        da.Fill(dt);

        if (dt.Rows.Count > 0) // Means first name is already present
        {
            lblmsg.Text = "This user is already added!";
        }
        else if (dt.Rows.Count == 0)
        {
            lblmsg.Visible = false;
            string username = tbName.Text;
            string pwd=tbPassword.Text;
            string Confirmpwd = tbConfirmPassword.Text;
            string Email = tbEmailID.Text;
            string department = ddlDepartment.SelectedValue;
            using (SqlCommand cmd = con.CreateCommand())
            {
                cmd.CommandText = "Insert into NewUser(UserName,Password,ConfirmPassword,EmailID,DepartmentName)values('" + tbName.Text + "','" + tbPassword.Text + "','"+tbConfirmPassword.Text+"','" + tbEmailID.Text + "','" + ddlDepartment.SelectedValue + "')";
                cmd.Parameters.AddWithValue("@FirstName", tbName.Text.Trim());
                cmd.Parameters.AddWithValue("@LastName", tbPassword.Text.Trim());
                cmd.Parameters.AddWithValue("@DomainID", tbConfirmPassword.Text.Trim());
                cmd.Parameters.AddWithValue("@EmailID", tbEmailID.Text.Trim());
                cmd.Parameters.AddWithValue(@"RoleType", ddlDepartment.SelectedValue);
                cmd.ExecuteNonQuery();
            }
            con.Close();
            tbName.Text = "";
            tbPassword.Text = "";
            tbConfirmPassword.Text = "";
            tbEmailID.Text = "";
            tbName.Focus();
        }
    }
}


now i have a search user page in which i have a textbox, gridview and search button when i enter name of any user and click on search button it will show user details inside gridview now i have an edit link inside gridview what i want is when i click on edit link it will redirect to createuser page where in place of submit button i want to show update button and when i make changes and click on update button details for the selected user which i select from edit link of search user page will update. how can i do that

aspx page of search user
C#
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" 
            GridLines="None">
            <AlternatingRowStyle BackColor="White" />
           <Columns>
                <asp:HyperLinkField DataNavigateUrlFields="ID" 
                DataNavigateUrlFormatString="~/CreateUser.aspx?ID={0}" 
                HeaderText="Edit" NavigateUrl="~/CreateUser.aspx" Text="Edit"/>
            </Columns>
            <EditRowStyle BackColor="#2461BF" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#EFF3FB" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F5F7FB" />
            <SortedAscendingHeaderStyle BackColor="#6D95E1" />
            <SortedDescendingCellStyle BackColor="#E9EBEF" />
            <SortedDescendingHeaderStyle BackColor="#4870BE" />
        </asp:GridView>
Now i have an edit link inside gridview what i want is when i click on edit link it will redirect to createuser page where in place of submit button i want to show update button and when i make changes and click on update button details for the selected user which i select from edit link of search user page will update. how can i do that

Thanks in advance
Posted
Updated 31-Jul-13 4:33am
v4
Comments
Sergey Alexandrovich Kryukov 31-Jul-13 10:14am    
Your tags are not informative. What "gridview"? what "button"? Exact fully-quialified type names, please. You need to tell us the type of your UI: the UI library or application type.
—SA

Amitesh,

You can try 2 things. 1st one is the quick and dirty method.

1. Set a session variable Session["CallerPage"]. Set that to "Add user"or "Edit User" depending upon which module you are launching the page from. Then set the button text in the page_Load method. In the Button Click Event you can check the Session variable value and create your If/else block according to them.

2. This one involves using the Query String. Whenever you are doing response.redirect add a querystring to the CreateUser page. Pass the parameter to check if the request is coming from Create module or Edit module. After you check the origin of request you can change the text and functionality of button as mentioned in the 1st option above.
 
Share this answer
 
v2
Comments
amitesh1989 2-Aug-13 3:59am    
I made some changes in my code but their is only one problem in it

I have a menu where i have two pages under user management tab.

1. create user

2. search user

i make some change in my code but their is one think that pamper me is when i click on edit link it redirect me to createuser page where submit button convert into update button with function.

Now when i click on create user page under user Management tab it show me update button so i cant create new user beacuse if btnsubmit.text=="Update" then it will execute update query.

Here is my code:

protected void btnSubmit_Click(object sender, EventArgs e)
{
if (btnSubmit.Text == "Submit")
{
this.Submit();
}
else if (btnSubmit.Text == "Update")
{
this.Update();
}
}

private void Submit()
{
con.Open();
da = new SqlDataAdapter();
dt = new DataTable();

string strSQL = "SELECT * FROM NewUser WHERE UserName = '" + tbName.Text + "'";

da.SelectCommand = new SqlCommand(strSQL, con);
da.Fill(dt);

if (dt.Rows.Count > 0) // Means first name is already present
{
lblmsg.Text = "This user is already added!";
}
else if (dt.Rows.Count == 0)
{
lblmsg.Visible = false;
string username = tbName.Text;
string pwd = tbPassword.Text;
string Confirmpwd = tbConfirmPassword.Text;
string Email = tbEmailID.Text;
string department = ddlDepartment.SelectedValue;
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "Insert into NewUser(UserName,Password,ConfirmPassword,EmailID,DepartmentName)values('" + tbName.Text + "','" + tbPassword.Text + "','" + tbConfirmPassword.Text + "','" + tbEmailID.Text + "','" + ddlDepartment.SelectedValue + "')";
cmd.Parameters.AddWithValue("@FirstName", tbName.Text.Trim());
cmd.Parameters.AddWithValue("@LastName", tbPassword.Text.Trim());
cmd.Parameters.AddWithValue("@DomainID", tbConfirmPassword.Text.Trim());
cmd.Parameters.AddWithValue("@EmailID", tbEmailID.Text.Trim());
cmd.Parameters.AddWithValue(@"RoleType", ddlDepartment.SelectedValue);
cmd.ExecuteNonQuery();
}
con.Close();
tbName.Text = "";
tbPassword.Text = "";
tbConfirmPassword.Text = "";
tbEmailID.Text = "";
tbName.Focus();
}
}
private void Update()
{
SqlCommand cmd;
String query;
con.Open();
query = "update NewUser set UserName='" + tbName.Text + "',Password='" + tbPassword.Text + "',ConfirmPassword='" + tbConfirmPassword.Text + "',EmailID='" + tbEmailID.Text + "',DepartmentName='" + ddlDepartment.SelectedValue + "' where ID=" + tbid.Text + "";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("~/SearchUser.aspx", false);
}
}
public string ButtonTextValue
{
get
{
if (Session["ButtonValue"] == null)
Session["ButtonValue"] = "Submit";

return Convert.ToString(Session["ButtonValue"]);
}
set
{
Session["ButtonValue"] = "Submit";
}
}

and on page load

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnSubmit.Text = ButtonTextValue;
}
}

and on search user page rowcommand of gridview has:

if (e.CommandName == "cmdBind")
{
LinkButton lb = (LinkButton)e.CommandSource;
Session["ButtonValue"] = "Update";
Response.Redirect("CreateUser.aspx?ID=" + lb.Text + "");
}
now i made some change and it work i am writing down it hope it work for some one who need it
on create user page ::

C#
protected void Page_Load(object sender, EventArgs e)
    {
         if (!IsPostBack)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["buttonValue"]))
            {
                    string btnValue = Request.QueryString["buttonValue"];
                    if (btnValue == "Update")
                    {
                        btnSubmit.Text = "Update";
                    }
                    else if (btnValue == "Submit")
                    {
                        btnSubmit.Text = "Submit";
                    }
                    else
                    {
                        Response.Write("error");
                    }
            }
                else
            {
                    this.btnSubmit.Text = "Submit";
            }
        
            string ID = Request.QueryString["ID"];
            cmd = new SqlCommand("Select * from NewUser where ID='" + ID + "'", con);
            con.Open();
            da = new SqlDataAdapter(cmd);
            dt.Clear();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                tbid.Text = ID;
                tbName.Text = dt.Rows[0][1].ToString();
                tbPassword.Text = dt.Rows[0][2].ToString();
                tbConfirmPassword.Text = dt.Rows[0][3].ToString();
                tbEmailID.Text = dt.Rows[0][4].ToString();
                ddlDepartment.SelectedValue = dt.Rows[0][5].ToString();
            }
        }
        con.Close();
    }


and on search user page made some change in gridview hyperlink

C#
 <asp:hyperlinkfield datanavigateurlfields="ID" headertext="Edit" xmlns:asp="#unknown">
                   DataNavigateUrlFormatString="~/CreateUser.aspx?ID={0}&buttonValue=Update"
                   NavigateUrl="~/CreateUser.aspx" Text="Edit" />
</asp:hyperlinkfield>

and it work
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900