Command Argument in Runtime created imagebutton doesn't work?
Hello i am creating new image button something like this ;
e.row.cells[0].text= string.empty; //clearing the cells
Imagebutton img1= new ImageButton();
img1.Id="imgInput";
img1.ImageUrl="~/sample.jpg";
img1.CommandArgument= varID; // fetching the ID
img1.click+= new ImageClickEventHandler(imgInput_Click);
e.row.controls.add(img1)
I have written the above code in gridview rowdatabound event which.
Now in the click event (imgButto开发者_开发百科n_Click; handled in second last line) i wish to put the command argument value in session;
protected void imgInput_Click(object sender, EventArgs e)
{
Session["idvalue"]= imgInput.CommandArgument;
}
But the varID here is coming as null. Why so? What am i missing here?
Please guide! Thanks
You need to use the ImageButton.Command event.
Example:
img1.Command += ImageButton_OnCommand;
protected void ImageButton_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "Sort" && e.CommandArgument == "Ascending")
Label1.Text = "You clicked the Sort Ascending Button";
else
Label1.Text = "You clicked the Sort Descending Button";
}
精彩评论