2015-04-25 71 views
2

我在中继器控件内的中继器控件和外部中有一个隐藏字段。下面是我有的asp.code。ASP.NET中继器控制 - 获得中继器控制内的Hiddenfield值

<asp:Repeater ID="rptAccordian" runat="server" OnItemDataBound="rptAccordian_ItemDataBound"> 
 
    <ItemTemplate> 
 
    <div class="s_panel"> 
 
     <h1> 
 
      <a href="#" data-content="Tool tip"><%# Eval("Name") %></a> 
 
     </h1> 
 
     <div> 
 
     <p> 
 
      <small><span style="font-family: 'Segoe UI'; font-weight: bold;">Category Objective: </span><span style="font-family: 'Segoe UI'"><%# Eval("Objective") %></span></small> 
 
     </p> 
 
     <p> 
 
      <small><span style="font-family: 'Segoe UI'; font-weight: bold;">Category Riskscore: </span> 
 
       <code><%# Eval("Score") %><span>%</span></code></small> 
 
     </p> 
 
     <p> 
 
      <code> 
 
       <img src="Content/img/add.png" /><asp:LinkButton ID="Add" runat="server">Add Question</asp:LinkButton> 
 
      </code> 
 
     </p> 
 
     <asp:HiddenField ID="hdnCategoryID" runat="server" Value='<%# Bind("CategoryID") %>' /> 
 
     </div> 
 
    </ItemTemplate> 
 
</asp:Repeater> 
 
<div id="modalpopup"> 
 
    <asp:Button ID="btnInsertQuestion" runat="server" Text="Save" OnClick="btnInsertQuestion_Click" /> 
 
</div>

我的后端代码如下。

protected void btnInsertQuestion_Click(object sender, EventArgs e) 
{ 
    HiddenField hf = (HiddenField)rptAccordian.FindControl("hdnCategoryID"); 
    catID = Convert.ToInt16(hf.Value); 
    Response.Write("ID is") + catID; 
} 

有13个中继器,每个中继器都有不同的CategoryID。我有一个链接按钮称为添加每个中继器,当我按下该按钮,我将打开一个模式弹出,它将有一个按钮。单击该按钮时,我需要显示属于该中继器控件的相应CategoryID,在该控件中单击ADD链接按钮。

但是,hiddenfield hf显示为空,我无法获得该手风琴隐藏字段的值。

+0

使用此链接将会有用。 http://stackoverflow.com/questions/29862571/dropdownlist-is-not-showing-the-selected-value –

+0

你正在寻找一个带有“hiddenid”id的'HiddenField',但你的aspx里的实际id是“ hdnCategoryID”。 – dario

回答

1

我已经使用jQuery获得了我的答案。我发现控件hdnCategoryID和它的值使用jQuery,我将该值分配给一个新的隐藏字段,并检索到我的点击事件的值。

2

你必须得到中继项目访问hiddenfield:

protected void btnInsertQuestion_Click(object sender, EventArgs e) 
     { 
      for (int i = 0; i < rptAccordian.Items.Count; i++) 
      { 
       var item = rptAccordian.Items[i]; 

       var hf = item.FindControl("hdnCategoryID") as HiddenField; 
       var val = hf.Value; 
      } 
     } 

修订

protected void Add_Click(object sender, EventArgs e) 
     { 
      var lb = sender as LinkButton; 
      var par = lb.Parent.FindControl("hdnCategoryID"); 

     } 
+0

嗨,试着用你的代码。然而,我在for(int i = 0; i

+0

它看起来像项集合为空。你是否在回发数据绑定中继器? – Legends

+0

是的。我在页面加载中绑定中继器(!this.IsPostBack) –

0

你可以得到不使用hiddenfield。你需要在这个中继器中声明Add LinkBut​​ton。

<asp:LinkButton ID="Add" runat="server" CommandArgument = '<%# Bind("CategoryID")'%> OnClick = "Add_Click">Add Question</asp:LinkButton>

你已经写代码btnInsertQuestion单击按钮,在这里,你要求得到CategoryID中添加按钮的点击,所以我假设你的要求是正确的,但您键入别的东西。

要在Add按钮点击中获得CategoryId,您需要这样写。

protected void Add_Click(object sender, EventArgs e) 
{ 
    //Get the reference of the clicked button. 
    LinkButton button = (sender as LinkButton); 

    //Get the command argument 
    string cat_id = button.CommandArgument; 
    // Type cast to int if application and use it. 
}