想在GridView控件来显示数据上点击链接时, [英] Want to show data in gridview when click on button
问题描述
我有两个文本框,一是下拉和两个按钮的。当我输入在文本框的值,从下拉列表中选择值和preSS 按钮1
,我的数据存储在数据库中,我可以看到,在我的网格时我点击按钮2
。
I have two textboxes, one dropdown and two button's. When I enter value in textboxes, select value from dropdown and press button1
, my data is stored in the database and I can view that in my grid when I click on button2
.
但问题是,当我preSS 按钮2
,而无需输入任何细节它应该显示验证错误,我在我的文本框和下拉菜单中添加。
But the problem is when I press button2
without entering any details it should show validation error which I add on my textboxes and dropdown.
code为aspx.cs
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=EMS;Integrated Security=True");
con.Open();
string UserName = TextBox1.Text;
string Password = TextBox2.Text;
string Role = DropDownList1.Text;
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "insert into Login(UserName,Password,Role) values('" + UserName + "','" + Password + "','" + Role + "')";
cmd.Parameters.AddWithValue("@UserName", TextBox1.Text);
cmd.Parameters.AddWithValue("@Password", TextBox2.Text);
cmd.Parameters.AddWithValue("@Role", DropDownList1.Text);
cmd.ExecuteNonQuery();
}
con.Close();
TextBox1.Text = string.Empty;
TextBox2.Text = string.Empty;
DropDownList1.Text = string.Empty;
TextBox1.Focus();
}
protected void Button2_Click1(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=EMS;Integrated Security=True");
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
SqlCommand com = new SqlCommand("Select * from Login", con);
SqlDataAdapter sda = new SqlDataAdapter(com);
DataSet ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
con.Close();
}
code为ASPX
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<style type="text/css">
.style1
{
width: 100%;
height: 178px;
}
.style3
{
width: 87px;
}
.style4
{
width: 87px;
height: 110px;
}
.style5
{
height: 110px;
}
</style>
</head>
<body style="height: 387px">
<form id="form1" runat="server">
<div>
<tr>
<td class="style3">
User name</td>
<td>
<asp:TextBox ID="TextBox1" runat="server" style="margin-left: 0px"> </asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="TextBox1" ErrorMessage="Enter User Name" Font- Bold="True">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
Password</td>
<td>
<asp:TextBox ID="TextBox2" runat="server" TextMode="Password"></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="TextBox2" ErrorMessage="Enter Password" Font- Bold="True">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style3">
Select Role</td>
<td>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>Select One</asp:ListItem>
<asp:ListItem>Admin</asp:ListItem>
<asp:ListItem>Manager</asp:ListItem>
<asp:ListItem>User</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="DropDownList1" ErrorMessage="Select Role">*</asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td class="style4">
</td>
<td class="style5">
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Sumit" />
<asp:ValidationSummary ID="ValidationSummary1" runat="server" Height="65px" />
</td>
</tr>
</table>
</div>
<p>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click1" Text="Show" />
</p>
<p>
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;
&NBSP;
推荐答案
请为第二个按钮的ValidationGroup属性设置为类似下面的任何值:
Please set the validationgroup property for second button to any value like below:
validationgroup="xyz"
和让我知道如果问题仍然存在。
and let me know if problem is still exists.
这篇关于想在GridView控件来显示数据上点击链接时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!