Reputation: 570
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
Upvotes: 2
Views: 346
Reputation: 46947
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()
Upvotes: 1