我有一个createuser页面,其中有一些字段和提交按钮。
当我点击我的提交按钮时,它会将详细信息保存到数据库中
createuser page的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里面有一个编辑链接我想要的是当我点击编辑链接时它将重定向到创建用户页面,代替提交按钮我想显示更新按钮,当我进行更改并点击更新按钮详细信息对于从搜索用户页面的编辑链接中选择的所选用户,将更新。我怎么能这样做?
答案 0 :(得分: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");
}
}
此提交中的是一个函数,其中插入查询工作和更新是更新查询工作的函数
在我的网格视图的搜索页面上,我在我的超链接中进行了一些更改
<asp:HyperLinkField DataNavigateUrlFields="ID" HeaderText="Edit"
DataNavigateUrlFormatString="~/CreateUser.aspx?ID={0}&buttonValue=Update"
NavigateUrl="~/CreateUser.aspx" Text="Edit" />
它对我有用