我一直在寻找这个答案,而我的问题没有任何解决方法。 我每次在按下按钮“添加”时,都会在页脚中有一个带下拉列表和文本框控件的gridview,这些控件的值会插入到gridview中。
我遇到的问题是下拉列表中的selectedindexchanged事件不起作用。我希望每次更改下拉选项时,文本框的值都会被填充一些值。
我已经在网上搜索了,我已经做了所有的事情
IMAGE.JPG This is my gridview with the dropdown control and the textbox boxes i want to fill
IMAGE.JPG The onselectedindexchange works only one time if i press de add button
这是我的html代码
<script runat="server">
Protected Sub Producto_SelectedIndexChanged(sender As Object, e As EventArgs)
Dim control As Control = Nothing
If grid1.FooterRow IsNot Nothing Then
control = grid1.FooterRow
Else
control = grid1.Controls(0).Controls(0)
End If
Dim descripcion As String
descripcion = "funciona"
CType(control.FindControl("descfac"), HtmlInputText).Value = descripcion
End Sub
Protected Sub Add(ByVal sender As Object, ByVal e As EventArgs)
Dim control As Control = Nothing
If grid1.FooterRow IsNot Nothing Then
control = grid1.FooterRow
Else
control = grid1.Controls(0).Controls(0)
End If
Dim producto As String = CType(control.FindControl("Producto"), DropDownList).Text
Dim dt As System.Data.DataTable = TryCast(ViewState("Factura"), System.Data.DataTable)
dt.Rows.Add(producto, "testitem", "testitem", "testitem", "testitem")
grid1.DataSource = dt
grid1.DataBind()
ViewState("Factura") = dt
End Sub
</script>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="udp1" updatemode="Always" runat="server">
<ContentTemplate>
<asp:GridView ID="grid1" runat="server" AutoGenerateColumns="false" ShowFooter="true" class="table table-hover">
<Columns>
<asp:TemplateField HeaderText="Producto">
<ItemTemplate>
<%# Eval("Producto") %>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList class="form-control select" runat="server" ID="producto"
data-live-search="true" AutoPostBack="true" ViewStateMode="Enabled" OnSelectedIndexChanged="producto_SelectedIndexChanged" EnableViewState="true">
<asp:ListItem>P00000050 - Zapatos de Mujer </asp:ListItem>
<asp:ListItem>P00000051 - Correa de Hombre</asp:ListItem>
<asp:ListItem>P00000052 - Camisa de niña</asp:ListItem>
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Descripción">
<ItemTemplate>
<%# Eval("Descripcion") %>
</ItemTemplate>
<FooterTemplate>
<input type="text" class="form-control" runat="server" id="descfac" placeholder="" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Cantidad">
<ItemTemplate>
<%# Eval("Cantidad") %>
</ItemTemplate>
<FooterTemplate>
<input type="text" class="form-control" runat="server" id="cantext" placeholder="" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Precio Unitario">
<ItemTemplate>
<%# Eval("Unitario") %>
</ItemTemplate>
<FooterTemplate>
<input type="text" class="form-control" runat="server" id="preunit" placeholder="" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Total">
<ItemTemplate>
<%# Eval("Total") %>
</ItemTemplate>
<FooterTemplate>
<input type="text" class="form-control" runat="server" id="detotal" placeholder="" disabled />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" class="btn btn-primary pull-left" CommandName="Footer" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EmptyDataTemplate>
<tr class="table table-hover">
<th scope="col">Producto
</th>
<th scope="col">Descripción
</th>
<th scope="col">Cantidad
</th>
<th scope="col">Precio Unitario
</th>
<th scope="col">Total
</th>
<th scope="col"></th>
</tr>
<tr>
<td>
<asp:DropDownList class="form-control select" runat="server" ID="producto"
data-live-search="true" AutoPostBack="true" ViewStateMode="Enabled" EnableViewState="true" OnSelectedIndexChanged="producto_SelectedIndexChanged">
<asp:ListItem>P00000050 - Zapatos de Mujer </asp:ListItem>
<asp:ListItem>P00000051 - Correa de Hombre</asp:ListItem>
<asp:ListItem>P00000052 - Camisa de niña</asp:ListItem>
</asp:DropDownList>
</td>
<td>
<input type="text" class="form-control" runat="server" id="descfac" placeholder="" />
</td>
<td>
<input type="text" class="form-control" runat="server" id="cantext" placeholder="" />
</td>
<td>
<input type="text" class="form-control" runat="server" id="preunit" placeholder="" />
</td>
<td>
<input type="text" class="form-control" runat="server" id="detotal" placeholder="" disabled />
</td>
<td>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Add" class="btn btn-primary pull-left" CommandName="EmptyDataTemplate" />
</td>
</tr>
</EmptyDataTemplate>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
我的代码后面
导入System.Data
公共类FacturaCliente 继承System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(4) {New DataColumn("Producto"), New DataColumn("Descripcion"),
New DataColumn("Cantidad"), New DataColumn("Unitario"), New DataColumn("Total")})
grid1.DataSource = dt
grid1.DataBind()
ViewState("Factura") = dt
End If
Dim control As Control = Nothing
If grid1.FooterRow IsNot Nothing Then
control = grid1.FooterRow
Else
control = grid1.Controls(0).Controls(0)
End If
Dim trig2 As AsyncPostBackTrigger = New AsyncPostBackTrigger()
trig2.ControlID = CType(control.FindControl("Producto"), DropDownList).UniqueID
trig2.EventName = "SelectedIndexChanged"
udp1.Triggers.Add(trig2)
ScriptManager1.RegisterAsyncPostBackControl(CType(control.FindControl("Producto"), DropDownList))
End Sub
结束班级
更新
我现在知道为什么在按下“添加”按钮后才触发该事件。有2个DDL,第一个是emptydatatemplate标记中的一个,另一个是gridview的页脚中的一个,出现在gridview中的第一行之后。
现在,在gridview行_databound事件中使用scriptmanager1.registerasynpostbackcontrol,页脚中的ddl可以正常工作。
问题仍然存在是在EmptyDataTemplate标记内的ddl中。我试图找到如何在DDL中激活SelectedIndexChanged(如果您有任何线索让我知道)。