本文介绍了Gridviews,选择命令+文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="MethodOfPayment" HeaderText="Method of Payment"
SortExpression="MethodOfPayment" />
</Columns>
</asp:GridView>
<br />
<br />
<asp:Label ID="Label1" runat="server" Text="Method of Payment:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True"
ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<br />
<asp:Label ID="Label2" runat="server" Text="Installment Amount:"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
</div>
</form>
我的网页上有这个。这是里面的代码。
I have this on my web page. Here are the codes inside.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection cn = new SqlConnection("Connection");
cn.Open();
string strSQL;
strSQL = "SELECT * FROM Orders";
SqlDataAdapter da = new SqlDataAdapter(strSQL, cn);
DataSet ds = new DataSet();
da.Fill(ds, "DB");
GridView1.DataSource = ds;
GridView1.DataBind();
cn.Close();
}
Label2.Visible = false;
TextBox2.Visible = false;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (TextBox1.Text == "Installment")
{
Label2.Visible = true;
TextBox2.Visible = true;
}
}
Textbox2和label2应该显示textbox1的值是'分期付款'。我做错了什么?谢谢!
Textbox2 and label2 are supposed to show if the value of the textbox1 is 'Installment'. What did I do wrong? Thank you!
推荐答案
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SqlConnection cn = new SqlConnection("Connection");
cn.Open();
string strSQL;
strSQL = "SELECT * FROM Orders";
SqlDataAdapter da = new SqlDataAdapter(strSQL, cn);
DataSet ds = new DataSet();
da.Fill(ds, "DB");
GridView1.DataSource = ds;
GridView1.DataBind();
cn.Close();
Label2.Visible = false;
TextBox2.Visible = false;
}
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
if (TextBox1.Text == "Installment")
{
Label2.Visible = true;
TextBox2.Visible = true;
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
}
Or
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
TextBox1.Text = GridView1.SelectedRow.Cells[1].Text;
TextBox1_TextChanged(sender, e);
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
if (TextBox1.Text == "Installment")
{
Label2.Visible = true;
TextBox2.Visible = true;
}
}
这篇关于Gridviews,选择命令+文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!