0

我有一个 createuser 页面,其中有一些字段和提交按钮。

当我点击我的提交按钮时,它会将详细信息保存到数据库中

createuser 页面的 aspx.cs 代码:

 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 中有一个编辑链接我想要的是什么时候我单击编辑链接,它将重定向到创建用户页面,代替提交按钮,我想显示更新按钮,当我进行更改并单击从搜索用户页面的编辑链接中选择的所选用户的更新按钮详细信息时,将更新. 我怎样才能做到这一点

搜索用户的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中有一个编辑链接我想要的是当我点击编辑链接时它会重定向到createuser页面代替提交按钮我想显示更新按钮以及当我进行更改并单击所选更新按钮的详细信息我从搜索用户页面的编辑链接中选择的用户将更新。我怎样才能做到这一点?

4

1 回答 1

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

和我的按钮的点击事件:

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

它对我有用

于 2013-08-06T11:07:31.040 回答