Repeater下拉事件触发修改本行数据
几行代码搞定,太漂亮了,记录总结下。 前台代码如下
<asp:Repeater ID="Repeater2" OnItemDataBound="Repeater2_OnItemDataBound" runat="server"> <ItemTemplate> <tr runat="server" id="row"> <td style="text-align: center"> <asp:DropDownList ID="ddlTFDate" Enabled="true" AutoPostBack="true" OnSelectedIndexChanged="ddlTFDate_TextChanged" runat="server"> <asp:ListItem Value="否">否</asp:ListItem> <asp:ListItem Value="是">是</asp:ListItem> </asp:DropDownList> </td> <td style="text-align: center"> <asp:TextBox ID="txtQJC12" Text='<%# Eval("QJC12")%>' runat="server" Width="80px"></asp:TextBox> </td> </tr> </ItemTemplate> </asp:Repeater>
后台代码如下:
protected void ddlTFDate_TextChanged(object sender, EventArgs e) { DropDownList ddl1 = ((DropDownList)sender).Parent.FindControl("ddlTFDate") as DropDownList; TextBox txt1 = ((DropDownList)sender).Parent.FindControl("txtQJC12") as TextBox; if (ddl1.SelectedItem.Text.Equals("是")) { txt1.Text = DateTime.Now.ToString("yyyy-MM-dd"); } else { txt1.Text = ""; } // 经过测试 CheckBox 无法获取到 直接 CheckBox 事件下 可以获取到 CheckBox cb_choose = (CheckBox)sender; RepeaterItem item =ddl1.NamingContainer as RepeaterItem; Repeater grd = item.NamingContainer as Repeater; CheckBox cb = item.FindControl("cbXJA16") as CheckBox; }
数据下拉加载绑定
protected void Repeater2_OnItemDataBound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType.Equals(ListItemType.Item) || e.Item.ItemType.Equals(ListItemType.AlternatingItem)) { DropDownList ddl = (DropDownList)e.Item.FindControl("ddlTFDate"); ListItem li = ddl.Items.FindByText(DataBinder.Eval(e.Item.DataItem, "QJC11").ToString().Trim()); if(li!=null) { ddl.ClearSelection(); li.Selected = true; } //ddl.SelectedItem.Value = DataBinder.Eval(e.Item.DataItem, "QJC11").ToString().Trim(); } }
你的一分支持,是我坚持创作的十分动力。
如果文章的内容对你有帮助的话,请用微信扫描下方二维码,支持一下。谢谢!