Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

c# - How to get value of label from within a Repeater on button click

Here is my asp.net Repeater From within it there is a button. I want to get the value of lblFaulType in a string in code behind when i click Button1, How can I do that?I have tried many approaches but nothing working. Please give me your suggestions.

<asp:Repeater ID="RepterDetails" runat="server">  
<HeaderTemplate>  
<table style="border:1px solid #0000FF; width:500px" cellpadding="0">     
</HeaderTemplate>  
<ItemTemplate>  
<tr style="background-color:#EBEFF0">  
<td>  
<table style="background-color:#EBEFF0;border-top:1px dotted #df5015; width:500px" >  
<tr>  
<td >  
<strong><asp:Label ID="Label18" runat="server" Text="Fault Category: " Font-Bold="true"/></strong>  
<asp:Label ID="lblFaultType" runat="server" Text='<%#Eval("Fault Type") %>' Font-Bold="true"/>  
</td>
<td >  
<strong><asp:Label ID="Label19" runat="server" Text="Fault Description: " Font-Bold="true"/></strong>  
<asp:Label ID="Label8" runat="server" Text='<%#Eval("Fault Description") %>' Font-Bold="true"/>  
</td> 
</tr> 
     <tr>  
<td >  
<strong><asp:Label ID="Label2" runat="server" Text="Building: " Font-Bold="true"/></strong>  
<asp:Label ID="Label9" runat="server" Text='<%#Eval("Name") %>' Font-Bold="true"/>  
</td>
<td >  
<strong><asp:Label ID="Label3" runat="server" Text="Floor: " Font-Bold="true"/></strong>  
<asp:Label ID="Label10" runat="server" Text='<%#Eval("FloorNo") %>' Font-Bold="true"/>  
</td> 
</tr>
 <tr>
     <td>
       <strong><asp:Label ID="Label6" runat="server" Text="Room Number: " Font-Bold="true"/></strong>  
     <asp:Label ID="Label11" runat="server" Text='<%#Eval("RoomNumber") %>' Font-Bold="true"/>
     </td>
 </tr>
     <tr>  
<td ><strong><asp:Label ID="Label14" runat="server" Text="Created Time: " Font-Bold="true"/></strong> <asp:Label ID="lblUser" runat="server" Font-Bold="true" Text='<%#Eval("Time") %>'/></td>  
<td ><strong><asp:Label ID="Label15" runat="server" Text="Created Date: " Font-Bold="true"/></strong><asp:Label ID="lblDate" runat="server" Font-Bold="true" Text='<%#Eval("Date") %>'/></td>  
</tr>
     <tr>  
<td ><strong><asp:Label ID="Label16" runat="server" Text="Start Status: " Font-Bold="true"/></strong>  <asp:Label ID="Label12" runat="server" Font-Bold="true" Text='<%#Eval("StartStatus") %>'/></td>  
<td ><strong><asp:Label ID="Label17" runat="server" Text="Assign Status: " Font-Bold="true"/></strong> <asp:Label ID="Label13" runat="server" Font-Bold="true" Text='<%#Eval("AssignStatus") %>'/></td>  
</tr>
</table>  
</td>  
</tr>  
<tr>  
<td >
 <asp:TextBox ID="lblComment"  runat="server" style="color:#000000;  font-weight: bold;" placeholder="Describe the fault here" Text='<%#Eval("Comment") %>' TextMode="MultiLine" Columns="70" Rows="6" Enabled="false"></asp:TextBox>
 
</td>  
</tr>      
<tr>  
<td colspan="2" class="text-center"><asp:Button ID="Button1" runat="server" Text="Assing Fault" class="btn btn-success"  OnClick="Button1_Click" /> </td>  
</tr>  
</ItemTemplate>  
<FooterTemplate>  
</table>  
</FooterTemplate>  
</asp:Repeater>
question from:https://stackoverflow.com/questions/65922196/how-to-get-value-of-label-from-within-a-repeater-on-button-click

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We may need to see the CodeBehind for this page, and specifically the "Button1_Click" method (click event).

Now though, it appears as though you only have one of the labels with an ID of "lblFaultType" (maybe you misspelled that in your initial description. Each of the other labels all carry sequentially numbered IDs (e.g. ID="Label1", ID="Label2", ID="Label3", etc), and your ClickEvent (the Button1_Click method) will need to iterate through those items to know 'which' label you are attempting to obtain a value from.

Suggestion: It may be better to apply a ClickEvent on each of the particular labels since it will have the ID there with it (a JScript/JQuery solution).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...