Answered by:
FindControl Returns Null

Question
-
User158811806 posted
In the code below, btn1 returns NULL. How do I fix it?
<div class="container-fluid"> <div class="first"> <div class="row"> <div class="col-lg-4"> <div id="div1" class="st-box" runat="server"> <button id="Button1" class="button" style="vertical-align:middle; width:100%" ><span>Button 1 </span></button> <p> This is a Test.... </p> </div> </div> ............. In Pagge_Load() { Button btn1 = (Button)Page.FindControl("Button1"); }
I have also tried the following but it won't find div1.Controls.
foreach (Control c in div1.Controls) { if (c.GetType() == typeof(Button)) { ((Button)c).Text = "MyButton"; } }
Saturday, July 4, 2020 5:04 AM
Answers
-
User-2054057000 posted
You will have to provide runat=server to your button:
<button runat="server" id="Button1" class="button" style="vertical-align:middle; width:100%" ><span>Button 1 </span></button>
Then simply access the button with it's id:
Button1.Text="abc";
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, July 4, 2020 5:10 AM
All replies
-
User-2054057000 posted
You will have to provide runat=server to your button:
<button runat="server" id="Button1" class="button" style="vertical-align:middle; width:100%" ><span>Button 1 </span></button>
Then simply access the button with it's id:
Button1.Text="abc";
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Saturday, July 4, 2020 5:10 AM -
User158811806 posted
Thanks for the quick response....
I had to use .InnerText to get it to work.
Saturday, July 4, 2020 5:29 AM -
User158811806 posted
I have run in to another issue with it...
My button caption is inside <span>Button 1</span> at design time, the css below works if I don't change button caption during Page_Load. But, If I change it at Page_load() then this css has not effect.
/* css */
.button span:after {
content: '\00bb';
;
opacity: 0;
top: 0;
right: -20px;
transition: 0.5s;
}Saturday, July 4, 2020 5:42 AM -
User158811806 posted
Never mind, I've fixed it uisng
spanButton1.InnerText = sLabel1;
Saturday, July 4, 2020 6:06 AM