我有一个转发器,其中包含不同的控件,如文本框、下拉列表等...这些控件的值填充在转发器的 itemdatabound 中。当我单击一个按钮时,我想从这些控件中获取值。此转发器位于具有母版页的页面中。
按键代码如下
protected void butEdit_Click(object sender, EventArgs e)
{
Location loc = new Location();
Dictionary<string, Dictionary<int, string>> ret = DBCommon.FillInterface(loc);
foreach (RepeaterItem repeated in repEdit.Items)
{
DropDownList drp = (DropDownList)repeated.FindControl("drpdown");
TextBox txt = (TextBox)repeated.FindControl("txt");
CheckBox chk = (CheckBox)repeated.FindControl("chk");
if (drp != null && !string.IsNullOrEmpty(drp.Attributes["ID"]))
{
loc.GetType().GetProperty(drp.Attributes["ID"].Split('#')[0] + "ID").SetValue(loc, int.Parse(drp.SelectedValue), null);
}
if (txt != null && !string.IsNullOrEmpty(txt.Attributes["ID"]))
{
if (txt.Attributes["ID"].Contains("#int"))
{
loc.GetType().GetProperty(txt.Attributes["ID"].Split('#')[0]).SetValue(loc, int.Parse(txt.Text), null);
}
else if (txt.Attributes["ID"].Contains("#decimal"))
{
loc.GetType().GetProperty(txt.Attributes["ID"].Split('#')[0]).SetValue(loc, decimal.Parse(txt.Text), null);
}
else
{
loc.GetType().GetProperty(txt.Attributes["ID"].Split('#')[0]).SetValue(loc, txt.Text, null);
}
}
if (chk != null && !string.IsNullOrEmpty(chk.Attributes["ID"]))
{
loc.GetType().GetProperty(chk.Attributes["ID"].Split('#')[0]).SetValue(loc, chk.Checked, null);
}
}
}
和aspx
<div class="searchResults">
<asp:Repeater ID="repEdit" runat="server" OnItemDataBound="repEdit_ItemDataBound" ViewStateMode="Enabled" EnableViewState="true" OnItemCommand="repEdit_ItemCommand">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Visible="false" ViewStateMode="Enabled" EnableViewState="true"></asp:Label>
<asp:TextBox ID="txt" runat="server" Visible="false" ViewStateMode="Enabled" EnableViewState="true"></asp:TextBox>
<asp:CheckBox ID="chk" runat="server" Visible="false" ViewStateMode="Enabled" EnableViewState="true"/>
<asp:DropDownList ID="drpdown" runat="server" Visible="false" EnableViewState="true"></asp:DropDownList>
</ItemTemplate>
<SeparatorTemplate>
<br />
</SeparatorTemplate>
<FooterTemplate>
<asp:Button runat="server" ID="butEdit" Visible="false" Text="Update" CommandArgument="editcomm" />
</FooterTemplate>
</asp:Repeater>
</div>