我在gridview中有3个文本框。在第一个和第二个文本框中输入值时,我要自动将结果写入第三个文本框中。
这是我的网格视图:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="892px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="Ales Puanı">
<ItemTemplate>
<asp:TextBox ID="Txt_Ales" runat="server" Text='<%#Eval("alesSinavPuani") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dil Puanı">
<ItemTemplate>
<asp:TextBox ID="txt_Dil" runat="server" Text='<%#Eval("dilSinavPuani") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Toplam">
<ItemTemplate>
<asp:Label ID="lbl_Toplam" runat="server" ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
答案 0 :(得分:0)
1)为2个文本框提及AutoPostBack =“ true” 2)2个文本框的提及事件OnTextChanged =“ Txt_TextChanged” 3)在TextChanged Event中访问控件,找到结果逻辑并将其分配给第3个控件。在这种情况下,第三个控件被称为“标签”。也可以是文本框。
ASPX代码
<asp:GridView ID="GridView1" runat="server" CellPadding="3" Width="892px" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2">
<Columns>
<asp:TemplateField HeaderText="Ales Puanı">
<ItemTemplate>
<asp:TextBox ID="Txt_Ales" runat="server" Text='<%#Eval("alesSinavPuani") %>' OnTextChanged="Txt_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Dil Puanı">
<ItemTemplate>
<asp:TextBox ID="txt_Dil" runat="server" Text='<%#Eval("dilSinavPuani") %>' OnTextChanged="Txt_TextChanged" AutoPostBack="true"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Toplam">
<ItemTemplate>
<asp:Label ID="lbl_Toplam" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
TextChange事件代码
protected void Txt_TextChanged(object sender, EventArgs e)
{
TextBox Txt_Ales = GridView1.Rows[((sender as TextBox).NamingContainer as GridViewRow).RowIndex].FindControl("Txt_Ales") as TextBox;
TextBox txt_Dil = GridView1.Rows[((sender as TextBox).NamingContainer as GridViewRow).RowIndex].FindControl("txt_Dil") as TextBox;
Label lbl_Toplam = GridView1.Rows[((sender as TextBox).NamingContainer as GridViewRow).RowIndex].FindControl("lbl_Toplam") as Label;
int num1, num2 = 0;
if (Txt_Ales != null && txt_Dil != null && lbl_Toplam != null)
{
int.TryParse(Txt_Ales.Text, out num1);
int.TryParse(txt_Dil.Text, out num2);
lbl_Toplam.Text = (num1 + num2).ToString();
}
}