我有一个包含不同控件的转发器,如文本框,下拉列表等... 这些控件的值填充在转发器的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>
答案 0 :(得分:0)
我使用Item.FindControl()
方法获取值。我需要在ItemCommand函数中获取值。
public void repEdit_Itemcommand(object source, RepeaterCommandEventArgs e)
{
txtField = (TextBox)e.Item.FindControl("txt");
txtField.Text;
}
像这样你也可以从转发器获得其他值。这对我有用,请试试这个。