0

2 つのテキスト ボックス、1 つのドロップダウンと 2 つのボタンがあります。テキストボックスに値を入力し、ドロップダウンから値を選択して を押す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>

                  

4

6 に答える 6

1

2 番目のボタンの validationgroup プロパティを次のような任意の値に設定してください。

validationgroup="xyz"

問題がまだ存在する場合はお知らせください。

于 2013-02-20T11:39:49.040 に答える
0

どこに書いたか確認

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

したがって、button2_click() でその値にアクセスすることはできません。テキスト ボックス コントロールが空であるため、検証エラーがスローされます。ブレークポイントを設定し、何が問題なのかを確認して修正します。

提案

nbspを使用します。for space in markup は、ブラウザーによってページを異なる方法でレンダリングします。使用することをお勧めします

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

インライン スタイル用。

于 2013-02-20T11:42:14.680 に答える
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();
     }
于 2013-02-21T10:51:29.807 に答える
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();
}
于 2013-10-07T12:09:02.983 に答える
0

ValidationGroup検証したいコントロールに与えることを試みることができます。
このプロパティを使用すると、特定のコントロールに対して検証を設定できます。
このようにしてみてください。これで問題が解決するかどうかをお知らせください。

于 2013-02-20T11:54:46.357 に答える
0

クライアント側で検証を行う場合は、ボタンの causevalidation プロパティを true に設定してみてください。

  Causesvalidation=true;
于 2013-02-20T11:55:28.807 に答える