2013-07-31 37 views
0

我有一個createuser頁面,其中我有一些字段並提交按鈕。如何更改按鈕的功能以及它的文本

當我點擊我的提交按鈕將信息保存到數據庫

aspx.cs代碼CREATEUSER頁:

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(); 
     } 
    } 
} 

現在我有,我有一個文本框搜索用戶頁面, GridView和搜索按鈕,當我輸入任何用戶的名稱,然後單擊搜索按鈕它將顯示gridview內的用戶詳細信息 現在我有一個編輯鏈接裏面gridview我想要的是當我點擊編輯鏈接它將重定向到createuser頁在哪裏提交按鈕的地方我想顯示更新按鈕,當我做出更改並點擊更新對接關於我從搜索用戶頁面的編輯鏈接中選擇的用戶的詳細信息將更新。我怎麼能做到這一點搜索用戶的

aspx頁面

<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> 

aspx.cs代碼:

protected void BindGrid() 
{ 
    if ((tbSearchUser.Text.Length == 0)) 
    { 
     lblMessage.Text = "Search Box cannot be empty! Please put something to search."; 
    } 
    con.Open(); 
    string query = "Select * from NewUser where UserName like'" + tbSearchUser.Text + "'"; 
    da = new SqlDataAdapter(query, con); 
    DataSet ds = new DataSet(); 
    da.Fill(ds); 
    GridView1.DataSource = ds; 
    GridView1.DataBind(); 
    GridView1.Dispose(); 
    con.Close(); 
} 
protected void btnSearchUser_Click(object sender, EventArgs e) 
{ 
    this.BindGrid(); 
} 

現在我有內部的GridView編輯鏈接我想要的是,當我點擊編輯鏈接它將重定向到創建用戶頁面在哪裏代替提交按鈕我想要顯示更新按鈕,當我進行更改並單擊更新按鈕的詳細信息選定用戶,我從搜索用戶頁面的編輯鏈接中選擇將更新。我怎樣才能做到這一點?

回答

0

這是我已經做了我工作,我想這也將會爲你們的工作以及

在我創建的用戶頁面我用戶的Request.QueryString這樣

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(); 
} 

和我的按鈕CLIK事件:

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

在此提交是一個函數,其中插入查詢工作,並更新是一個函數,其中更新查詢工作

,並在我的GridView我的搜索頁面上我做我的超級鏈接這樣

<asp:HyperLinkField DataNavigateUrlFields="ID" HeaderText="Edit" 
       DataNavigateUrlFormatString="~/CreateUser.aspx?ID={0}&buttonValue=Update" 
       NavigateUrl="~/CreateUser.aspx" Text="Edit" /> 

一些變化,它爲我工作