Question

I have a repeater control in my aspx page and in that page i have placed checkbox. When a user checks this box, then i want to redirect to a page. I have written a javaScript too to perform this action as follows:

js:

function update(eid) {
        window.location("Events.aspx?eid="+eid);
    }

following is method in code behind:

protected void rptEventReminder_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        CheckBox cbx = e.Item.FindControl("chkComplete") as CheckBox;
        Label lbl = e.Item.FindControl("lblEid") as Label;

        if (cbx != null && lbl !=null)
        {
            Int64 eid = Convert.ToInt64(lbl.Text);
            cbx.Attributes.Add("onclick", "update(eid);");
        }
    }

that eid which i am passing as parameter is the unique in database.

javaScript Error i am getting is:

JavaScript runtime error: 'eid' is undefined

Was it helpful?

Solution

Currently, You are passing eid as hard-coded text to onclick handler, Which treats it as JavaScript variable thus you are getting an error

JavaScript runtime error: 'eid' is undefined

Now, In C# code eid is a variable thus you need to pass it as

cbx.Attributes.Add("onclick", "update(" + eid +");");

OTHER TIPS

I found solution to my question. I made little changes in code behind where i am calling a javaScript as follows:

protected void rptEventReminder_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    CheckBox cbx = e.Item.FindControl("chkComplete") as CheckBox;
    Label lbl = e.Item.FindControl("lblEid") as Label;

    if (cbx != null && lbl !=null)
    {
        Int64 eid = Convert.ToInt64(lbl.Text);
        cbx.Attributes.Add("onclick", "update("+eid+");");
    }
}

now its working fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top