.NET ---- 借助repeater在行中进行下拉框编辑 (前端赋值给下拉框)
需求:在本行中进行下拉框编辑
实现
1、借助 onItemDataBound 事件绑定 RepeaterCOrder_ItemDataBound 方法
Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand
- ItemDataBound:数据绑定的时候(正在进行时)发生。
- ItemCommand :用来响应Item模板中的控件的事件。
借助的控件,
asp:HiddenField 控件给修改后的下拉框赋值
asp:DropDownList 控件进行编辑
asp:ListItem 下拉框中有哪些值
因为分为编辑和查看两种形式,所以在查看的时候使用了asp:Label 进行绑定,
asp:DropDownList 与 asp:Label 通过 Visible="false/true" 属性控制显示与隐藏
<asp:Repeater ID="RepeaterCOrder" runat="server" onitemdatabound="RepeaterCOrder_ItemDataBound"> <ItemTemplate> <tr> <td style="white-space: nowrap" align="center" ><%--是否有进货支出,0否1是--%> <asp:HiddenField ID="HiddenFielIs_InStock" runat="server" Value='<%# DataBinder.Eval(Container.DataItem,"Is_InStock") %>' /> <asp:DropDownList ID="ddlIs_InStock" runat="server" width="71px"> <asp:ListItem Text="否" Value="0"></asp:ListItem> <asp:ListItem Text="是" Value="1"></asp:ListItem> </asp:DropDownList> <%-- <asp:TextBox ID="tbIs_InStock" Visible="true" runat="server" Text='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Is_InStock")) == 1 ? "是" :"否" %>'></asp:TextBox>--%> <asp:Label Visible="false" ID="LabelIs_InStock" runat="server" Text='<%# Convert.ToInt32(DataBinder.Eval(Container.DataItem,"Is_InStock")) == 1 ? "是" :"否" %>'></asp:Label> </td> </tr> </ItemTemplate> </asp:Repeater>
后端在页面加载的时候需要完成上述
{//是否有进货支出,0否1是 //获取隐藏域中的 HiddenField HiddenFielIs_InStock = (HiddenField)e.Item.FindControl("HiddenFielIs_InStock"); DropDownList ddlIs_InStock = (DropDownList)e.Item.FindControl("ddlIs_InStock"); // TextBox tbIs_InStock = (TextBox)e.Item.FindControl("tbIs_InStock"); //将隐藏域中的值赋值给下拉框 if(ddlIs_InStock != null && HiddenFielIs_InStock != null) {
//ddlLoad_Files(ddlDealer_Level_4053_Guid,"Dealer_Level_4053_Guid"); //后端给下拉框的下拉项 ListItem 赋值 ,封装个 ddlLoad_Files(DropDownList ddl,string sFilesKey) 方法统一赋值 ddlIs_InStock.SelectedValue = HiddenFielIs_InStock.Value; } //修改下拉框样式 if(ddlIs_InStock != null) { base.SetDropDownListStyleLineBlack(ddlIs_InStock,51); } //控制显示隐藏 if(_Edit == "show") { Label LabelIs_InStock = (Label)e.Item.FindControl("LabelIs_InStock"); if(LabelIs_InStock != null && ddlIs_InStock != null) { ddlIs_InStock.Visible = false; LabelIs_InStock.Visible = true; } } }
2、保存的时候
单个
<asp:LinkButton ID="LinkButtonSave" runat="server" CommandName='<%# Container.ItemIndex %>' CommandArgument='<%# DataBinder.Eval(Container.DataItem,"iNumber") %>' OnClick="Rpbtn_SaveAll_Click"><img src="../../qdadmin/qdimages/Save.png" border="0" title="保存"></asp:LinkButton>
批量
<asp:LinkButton ID="LinkButtonSave" runat="server" OnClick="Rpbtn_SaveAll_Click"><img src="../../qdadmin/qdimages/edit_24_bc_red.png" border="0" title="批量保存"></asp:LinkButton>
先调用
protected void Rpbtn_SaveAll_Click(object sender,EventArgs e) { LinkButton btn = (LinkButton)sender; if(Rpbtn_SaveAll(btn.CommandArgument) > 0) { WebMessageBox1.MessageBox_tishi(Master,300,200,"成功更新成功"); HtmlTableLoad(_iPagePrve); } else { WebMessageBox1.MessageBox_tishi(Master,300,200,"数据更新失败"); } }
统一调用
protected int Rpbtn_SaveAll(string sindexORguid) { LabelEditMsg.Text = string.Empty; ITEM.Inherits.Bll.qdMis4.bllCOrder _bllcorder = new ITEM.Inherits.Bll.qdMis4.bllCOrder(); int k = 0; List<string> strsqlList = new List<string>(); for(int i = 0; i <= this.RepeaterCOrder.Items.Count - 1; i++) { Label LabeliNumber = (Label)this.RepeaterCOrder.Items[i].FindControl("LabeliNumberX"); HiddenField HidOrderGuidX = (HiddenField)this.RepeaterCOrder.Items[i].FindControl("HidOrderGuid"); if(LabeliNumber == null || HidOrderGuidX == null) continue; if(sindexORguid != string.Empty) { if(!(sindexORguid == LabeliNumber.Text || sindexORguid == HidOrderGuidX.Value)) continue; } string strsql = "update tbCOrder set "; HiddenField HidOrderGuid = (HiddenField)this.RepeaterCOrder.Items[i].FindControl("HidOrderGuid"); if(HidOrderGuid == null || HidOrderGuid.Value == "") continue; string sign = string.Empty; {//是否有进货支出,0否1是 DropDownList ddlIs_InStock = (DropDownList)this.RepeaterCOrder.Items[i].FindControl("ddlIs_InStock"); // TextBox tbIs_InStock = (TextBox)this.RepeaterCOrder.Items[i].FindControl("tbIs_InStock"); try { int d = Convert.ToInt32(ddlIs_InStock.SelectedValue); strsql += sign + "Is_InStock=" + d + ""; sign = ","; } catch(Exception ex) { LabelEditMsg.Text = "请输入是否有进货支出,0否1是"; return -1; } } }