How to access id of control defined in cs page [Resolved]

Posted by pranaykssoftech-26135 under ASP.NET on 3/11/2014 | Points: 10 | Views : 1640 | Status : [Member] | Replies : 3
Hi,

I am trying to design some part of aspx page in cs file. Suppose if aspx page is like -

--------------------------------------------------------------------------------------
<div>
<img id="imgLogo1" alt="" runat="server" style="width: 70px;" />
</div>
--------------------------------------------------------------------------------------

I want to do this code in cs page like -

--------------------------------------------------------------------------------------
string strBody = "<div>";
strBody += "<img id='imgLogo1' alt='' runat='server' style='width: 70px;' />";
strBody += "<div>";
--------------------------------------------------------------------------------------

Now this strBody, I will use in my aspx design page, but the question is how can I access "imgLogo1" in my code file.
If it is define in aspx page, then it would be simple, but I have to define in cs page only.
So can anybody help me, how can I access any id which is runat server but define in cs page.

Thanks in advance.




Responses

Posted by: A2H on: 3/11/2014 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
Hi,
I guess your requirement is to access the dynamically generated control at server side by using their ids. You can easily acheieve your requirement with a slight modification in your approach.
I would suggest you to have the div as a control place holder in aspx page itself. later to this div you can add the control and you can find the controls inside div using the FindControl method.

Please see the sample implementation
First of all add the below namespace to your page
using System.Web.UI.HtmlControls;
Then you can use the below code to generate the img control dynamically.
            //Creating html img control

HtmlImage img = new HtmlImage();
//Assign id to img control
img.ID = "imgLogo1";
//Assign alt text to img control
img.Alt = "";
//Assign source to img control
img.Src = "";
//Assign width to img control
img.Style["width"] = "70px";
//Add the control to the div which is a place holder for
divControlPlaceholder.Controls.Add(img);


once you create and add the control to your div. then you can access the controls like given below

 protected void Button1_Click(object sender, EventArgs e)

{
//Get reference of image control which is inside the content place holder div
HtmlImage img1 = (HtmlImage)divControlPlaceholder.FindControl("imgLogo1");
//Assign a new image to the control
img1.Src = "YourImagePath/Image1.jpg";
}


In my aspx page I have declared div which is the content placeholder for the controls

<div id="divControlPlaceholder" runat="server">


</div>


Hope this helps.

Thanks,
A2H
My Blog

pranaykssoftech-26135, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: pranaykssoftech-26135 on: 3/12/2014 [Member] Starter | Points: 25

Up
0
Down
It works fine.
Thanx a lot.

pranaykssoftech-26135, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: A2H on: 3/12/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
Glad to be of help.
Happy Coding.......

Thanks,
A2H
My Blog

pranaykssoftech-26135, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response