user614946
user614946

Reputation: 619

Passing control id to javascript from code behind

I am making a dynamic image gallary in which images of a folder are listed in div.
I want to increase there size on onclick by javascript.

I am assigning onclick event from code-behind like this

   int count = ds.Tables[0].Rows.Count; ;
             for (int i = 0; i < count; i++)
            {
                ImageButton img = new ImageButton();
                img.ID = "projectimg" + i.ToString();
                img.ImageUrl = ds.Tables[0].Rows[i][0].ToString();
                img.Height = 120;
                img.Width = 120;
                img.ToolTip = ds.Tables[0].Rows[0][1].ToString();
                img.OnClientClick = "incsize('" + "contentPlace_" + img.ID + "')";
              }

where incsize is my function in JavaScript

function incsize(imgid)
{
    alert("jjj");
    //do some thing here as
    var img = document.getElementById(imgid);
}

But this is not working. What is wrong with this code?

Upvotes: 1

Views: 2217

Answers (2)

codeandcloud
codeandcloud

Reputation: 55298

Try this.

Suppose you have an Image in your aspx like this.

<asp:Image ID="MyImage" runat="server" ImageUrl="~/some-image.png"/>

Assign a JavaScript oonclick attribute like this on code behind like this.

string script = String.Format("javascript:return incsize('{0}');", MyImage.ClientID);
MyImage.Attributes.Add("onclick", script);

Now call it in JavaScript like this.

function incsize(imgid) {
    //alert("jjj");
    //do some thing here as
    var img = document.getElementById(imgid);
}


Please ask clearly what you want in your future questions.

Do this inside your loop.

ImageButton img = new ImageButton(); //Image img = new Image(); 
img.ID = "projectimg" + i.ToString(); 
img.ImageUrl = ds.Tables[0].Rows[i][0].ToString(); 
img.Height = 120; 
img.Width = 120; 
img.ToolTip = ds.Tables[0].Rows[0][1].ToString();
img.ClientIDMode = System.Web.UI.ClientIDMode.Static;
string script = String.Format("javascript:return incsize('{0}');", img.ID);
img.Attributes.Add("onclick", script);

Upvotes: 1

ahoo
ahoo

Reputation: 1361

test this:

img.OnClientClick = "incsize('" + "contentPlace_" + img.ID + "')";

you had forget single qutation mark

Upvotes: 0

Related Questions