想要在点击按钮时在gridview中显示数据

时间:2013-02-20 11:06:01

标签: asp.net validation gridview

我有两个文本框,一个下拉列表和两个按钮。当我在文本框中输入值时,从下拉列表中选择值并按button1,我的数据存储在数据库中,当我点击button2时,我可以在我的网格中查看。

但问题是当我按button2而没有输入任何细节时,它应该显示我在文本框和下拉列表中添加的验证错误。

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

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">
          &nbsp;User name</td>
            <td>
         <asp:TextBox ID="TextBox1" runat="server" style="margin-left: 0px"> </asp:TextBox>
              &nbsp;&nbsp;
                <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>
    &nbsp;&nbsp;
                <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>
                &nbsp;</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>
                             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Button ID="Button2" runat="server" onclick="Button2_Click1" Text="Show" />
                &nbsp;</p>
<p>

                                                                                                                                                                                                                                                                                                            

    

6 个答案:

答案 0 :(得分:1)

请将第二个按钮的validationgroup属性设置为如下所示的任何值:

validationgroup="xyz"

如果问题仍然存在,请告诉我。

答案 1 :(得分:0)

检查你在哪里写

string.Empty; -> This is making your textbox empty.

因此,您将无法在button2_click()上访问其值。由于文本框控件为空,因此抛出验证错误。有断点并检查出了什么问题并纠正它。

<强>建议

在标记中使用 nbsp; 表示空间会在不同的浏览器中呈现不同的页面。我建议你使用

<span style="margin:0 0 0 20px"> Some text here</span> 

用于内联样式。

答案 2 :(得分:0)

您可以尝试将ValidationGroup提供给您要验证的控件 通过使用此属性,您可以设置对特定控件的验证 试着这样做。如果这样可以解决您的问题,请告诉我。

答案 3 :(得分:0)

如果您希望在客户端进行验证,请尝试将causevalidvalid属性设置为true。

  Causesvalidation=true;

答案 4 :(得分:0)

标记为DELETE:[我添加了我的样本,没关系改变它]

<asp:TemplateField HeaderText="modify">
  <ItemTemplate>
    <asp:ImageButton ID="imgmodify" CommandName="modify" runat="server" ImageUrl="~/database/images/edit.jpg" ToolTip="Edit" Height="20px" Width="20px"/>

 </ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Remove">
  <ItemTemplate>
  <asp:ImageButton ID="imgbtnDelete" CommandName="Delete" CommandArgument='<%#Eval("catID") %>' runat="server" ImageUrl="~/database/images/DeleteRed.jpg" ToolTip="Delete" Height="20px" Width="20px" />
</ItemTemplate>
</asp:TemplateField>

代码:

 protected void gvcat_rowcmd(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "modify")
    {
        //Your edit code here..
         Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "editpopup();", true);
    }
    if (e.CommandName == "Delete")
    {
        //Your delete code here
        categoryID = Convert.ToInt32(e.CommandArgument);
        Session["cat"] = categoryID;
        Page.ClientScript.RegisterStartupScript(this.GetType(), "Call my function", "showpopup();", true);
    }
}

删除行:

 if (e.CommandName == "delete")
    {
      int productcode = Convert.ToInt32(e.CommandArgument);
        string Cond = "Pid=" + productcode;
        DataRow[] mrow = dtCartItems.Select(Cond);
        if (mrow.Length > 0)
        {
            mrow[0].Delete();
        }
        dtCartItems.AcceptChanges();
        mrow = dtCartItems.Select();
        gridbind();
     }

答案 5 :(得分:0)

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