2

I have function in javascript with argument and I want to call this function multiple times when Gridview bind its data. so I have put the code like this

if (e.Row.RowType == DataControlRowType.DataRow)
{
    if (((DataRowView)e.Row.DataItem) != null)
 {
Page.ClientScript.RegisterStartupScript(this.GetType(),new Random().Next(100).ToString(), 
                        "likeStatus('"+argument+"')", true);
 }
}

Each time I change the value of key but this function is called only once. so please help me what should I do to call function in each iteration of gridview binding.

Thanks in advance

4
  • 1
    make the key dynamic, so that each script in page is registered with a unique id, it shall then be called. Registering with the same key prohibits multiple script registrations. Commented Nov 10, 2012 at 10:18
  • I have also used random key but it doesn't work Commented Nov 10, 2012 at 10:21
  • Please use Unique key everytime...@BrijeshGandhi Commented Nov 10, 2012 at 10:33
  • Mr.Vishal have u not seen I have already told that I am using random key Commented Nov 10, 2012 at 10:36

1 Answer 1

1

The problem is that if you need randomness you need to use the same instance of Random and can't create a new one each time. The way you are doing it right now might generate the same value every time. (Also note that a random value is not the same as a unique value)
To solve the problem I would however do thing a little differently.

Declare a StringBuilder as a field in you class. Create it before you bind the grid:

sb = new StringBuilder();
gridView.DataBind();

Then in the RowDataBound event of the GridView write to the builder.

if (e.Row.RowType == DataControlRowType.DataRow)
{
    if (((DataRowView)e.Row.DataItem) != null)
       sb.Append("likeStatus('"+argument+"');");
}

Finally in PreRender register the script string

Page.ClientScript.RegisterStartupScript(this.GetType(), "MyScript", 
                        sb.ToString(), true);

Alternatively use a unique value as the key, for example Guid.NewGuid().ToString()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.