我有一个有趣的问题。我的页面上有一个转发器:
<asp:Repeater runat="server" ID="rpt" OnItemDataBound="rpt_ItemDataBound">
<ItemTemplate>
<asp:Label runat="server" ID="lblRptKey" Text='<%#Eval("ID")%>' Visible="false"></asp:Label>
<asp:HiddenField runat="server" ID="Hidden1" ClientIDMode="Static" />
<div class="form-group">
<label class="control-label col-md-3"><%# DataBinder.Eval(Container.DataItem, "Name") %></label>
<div class="col-md-9">
<div class="media">
<asp:Label runat="server" ID="ex1SliderVal" CssClass="pull-right" Font-Bold="true">5</asp:Label>
<div class="media-body">
<asp:TextBox runat="server" ID="ex1" type="text" data-slider-min="0" data-slider-max="10" data-slider-step="1" data-slider-value="5" data-slider-handle="square" />
</div>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
我正在更改转发器的ItemDataBound事件中某些控件的ID,这对我的javascript函数是必需的。它们应该是彼此不同的。
protected void rpt_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
var input = e.Item.FindControl("ex1");
input.ClientIDMode = ClientIDMode.Static;
input.ID = "ex" + (e.Item.ItemIndex + 1).ToString();
var label = e.Item.FindControl("ex1SliderVal");
label.ClientIDMode = ClientIDMode.Static;
label.ID = "ex" + (e.Item.ItemIndex + 1).ToString() + "SliderVal";
var hidden = e.Item.FindControl("Hidden1");
hidden.ID = "Hidden" + (e.Item.ItemIndex + 1).ToString();
}
这是问题部分。我无法通过我之前更改过的ID来获取HiddenFields的值。
protected void btnSave_Click(object sender, EventArgs e)
{
NHibernateDaoFactory DaoFactory=new NHibernateDaoFactory();
SportType_User stu = new SportType_User();
stu.User = loginUser;
stu.SportType=DaoFactory.GetSportTypeDao().GetById(Convert.ToInt32(lblKey.Text),false);
foreach (RepeaterItem item in rpt.Items)
{
Label lblRptKey = (Label)item.FindControl("lblRptKey");
int ProfessionId=Convert.ToInt32(lblRptKey.Text);
Profession profession = DaoFactory.GetProfessionDao().GetById(ProfessionId,false);
HiddenField hidden = (HiddenField)item.FindControl("Hidden"+(item.ItemIndex+1).ToString());//This is returning right for first. But then it is returning null
int val = Convert.ToInt32(hidden.Value);
val = val > 10 ? 10 : (val < 0 ? 0 : val);
ProfessionValue pv = new ProfessionValue();
pv.Profession = profession;
pv.Value = val;
stu.ProfessionValues.Add(pv);
}
stu.SelfValued = true;
DaoFactory.GetSportType_UserDao().Save(stu);
Response.Redirect("EditSports.aspx");
}
上面方法中的隐藏字段向右返回第一个项目,但随后返回null