Gridview RowCommand arguments not passing correctly in Safari - asp.net

I have an asp page with a gridview on. I have also added a clientscript to each bound row to highlight/un-higlight on mouse over/out. I have added an asp:button as a templatefield and bound a value to the CommandArgument. In IE and FIrefox I get the expected behaviour where the CommandName is passed to the _RowCommand event. However, when in Safari I only ever see the CommanName of "Select" passed to RowCommand.
The expected behaviour is that when a bound row is clicked the "Select" paramater is passed to the RowCommand event. When the Button in the row is clicked the argument "Remove" is passed.
protected void gvContacts_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onMouseOver", "Highlight(this)");
e.Row.Attributes.Add("onMouseOut", "UnHighlight(this)");
e.Row.Attributes["onclick"] = Page.ClientScript.GetPostBackClientHyperlink(gvContacts, "Select$" + e.Row.RowIndex);
e.Row.Attributes["style"] = "cursor:pointer";
}
}
protected void gvContacts_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName) //Always "Select" when browser is Safari.
{
case "Select":
Session["clientID"] = gvContacts.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text;
Response.Redirect("../Contacts/ContactEdit.aspx?readin=1");
break;
case "Remove":
//Remove the client from the list
Company company = new Company();
company.Get(Int32.Parse(Session["CompanyID"].ToString()), ((Model)Session["model"]).ConnectionString);
company.RemoveUser(Int32.Parse(e.CommandArgument.ToString()));
BindGrid(company.ID);
break;
}
}
HTML for itemtemplate
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnRemove" runat="server" Text="Remove" CommandName="Remove" OnClientClick="return confirmRemove();"
CommandArgument='<%# Eval("ID") %>' />
</ItemTemplate>
Any thoughts would be appreciated. Thanks

I think You should First Check that Button was clicked From which browser and if it was from safari than Change Command Name ..
protected void gvContacts_RowCommand(object sender, GridViewCommandEventArgs e)
{
System.Web.HttpBrowserCapabilities browser = Request.Browser;
if(browser.Browser=="Safari") //Check The Browser
{
e.CommandName="Select";
}
switch (e.CommandName) //Always "Select" when browser is Safari.
{
case "Select":
Session["clientID"] = gvContacts.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Text;
Response.Redirect("../Contacts/ContactEdit.aspx?readin=1");
break;
case "Remove":
//Remove the client from the list
Company company = new Company();
company.Get(Int32.Parse(Session["CompanyID"].ToString()), ((Model)Session["model"]).ConnectionString);
company.RemoveUser(Int32.Parse(e.CommandArgument.ToString()));
BindGrid(company.ID);
break;
}
}

Related

Repeater with Buttons doesn't fire OnItemCommand even when DataBind doesn't happen when IsPostBack is true

I've got an image button in a Repeater, which is in a User Control, and I can't get the OnItemCommand event to fire when I click it. It always gives the error: "... Invalid postback or callback argument. Event validation is enabled using ..."
When I change the image button to a link button, it doesn't give me the error, but it still doesn't fire the OnItemCommand function.
I found some other relevant answers (such as How to process events from Buttons inside Repeaters? And what's this EnableEventValidation thing?) but they all say, "Make sure you're data-binding your repeater inside of a if(!Page.IsPostBack) block." I have done that, but it didn't make a difference.
Here's the markup for the repeater:
<asp:Repeater ID="rptExpenses" OnItemDataBound="rptExpenses_ItemDataBound" OnItemCommand="Button_Command" runat="server" >
<ItemTemplate>
<asp:ImageButton ID="ibDelete" ImageUrl="~/Images/delete.png" CommandName="Delete" runat="server" />
</ItemTemplate>
</asp:Repeater>
Here's some exerpts from the code-behind
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
rptExpenses.DataSource = ExpenseIds;
rptExpenses.DataBind();
}
}
protected void rptExpenses_ItemDataBound(object sender, RepeaterItemEventArgs e) {
if ((e.Item.ItemType == ListItemType.Item) || (e.Item.ItemType == ListItemType.AlternatingItem)) {
var ibDelete = (ImageButton)e.Item.FindControl("ibDelete");
ibDelete.CommandArgument = e.Item.DataItem.ToString();
}
}
protected void Button_Command(object sender, EventArgs e) {
var btn = (IButtonControl)sender;
switch (btn.CommandName) {
case "Delete":
//delete it
break;
}
}
Try using this code instead:
<asp:Repeater ID="rptExpenses" runat="server">
<ItemTemplate>
<asp:ImageButton ID="ibRemove" ImageUrl="~/Images/delete.png" runat="server"
CommandName="Remove"
CommandArgument='<%# Container.DataItem %>'
OnCommand="ibRemove_Click"/>
</ItemTemplate>
</asp:Repeater>
And this in the code behind:
protected void Page_Load(object sender, EventArgs e) {
if (!IsPostBack) {
rptExpenses.DataSource = ExpenseIds;
rptExpenses.DataBind();
}
}
protected void ibRemove_Click(object sender, CommandEventArgs e)
{
var btn = (IButtonControl)sender;
switch (btn.CommandName)
{
case "Remove":
//delete it, use btn.CommandArgument to find id to remove
break;
}
}
If you still receive the ... Invalid postback or callback argument. Event validation is enabled using ... error, it most likely means that you are somewhere re-binding the repeater before the ibRemove_Click(...) event is raised.
I found the reason why this was happening in the answer to this question: Why ItemCommand doesn't fired on My Repeater
Turns out that for some reason the Page's DataBind function was invalidating the repeater's controls without passing through the repeater's OnItemDataBound function a second time.

Radio button doesn't get selected after a post back

I have an item template within repeater:
<ItemTemplate>
<li>
<input type="radio"
value="<%# GetAssetId((Guid) (Container.DataItem)) %>"
name="AssetId"
<%# SelectAsset((Guid) Container.DataItem) %> />
</li>
</ItemTemplate>
I have a method that compares ids and decides whether to check the radio button.
protected string SelectAsset(Guid uniqueId)
{
if (uniqueId == GetSomeId())
return "checked=\"checked\"";
return string.Empty;
}
SelectAsset gets hit, but it doesn't select a radio button on a post back, but it does work if I just refresh the page. What am I doing wrong here?
Answer here: How to display "selected radio button" after refresh? says that it's not possible to achieve, is this really the case?
Thank you
Update
It appears that view state isn't available for simple controls if they don't have a runat attribute. I have solved this by using a custom GroupRadioButton control. Thank you for your help.
I'd suggest using a RadioButtonList:
Page Code
<asp:RadioButtonList RepeatLayout="UnorderedList" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true" ID="RadioRepeater" runat="server" />
<asp:Label ID="SelectedRadioLabel" runat="server" />
Code Behind
if (!Page.IsPostBack)
{
/* example adds items manually
- you could iterate your datasource here as well */
this.RadioRepeater.Items.Add(new ListItem("Foo"));
this.RadioRepeater.Items.Add(new ListItem("Bar"));
this.RadioRepeater.Items.Add(new ListItem("Baz"));
this.RadioRepeater.SelectedIndex = this.RadioRepeater.Items.IndexOf(new ListItem("Bar"));
this.RadioRepeater.DataBind();
}
protected void IndexChanged(object sender, EventArgs e)
{
this.SelectedRadioLabel.Text = string.Format("Selected Item Text: {0}", this.RadioRepeater.SelectedItem.Text);
}
I assume you only need to select one item.
As described in the comments, it even works to access the SelectedItem in the Page_Loadevent handler:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// previous code omitted
}
else
{
string foo = this.RadioRepeater.SelectedItem.Text;
}
}
If you are creating all your controls dynamically at run-time (directly from code), then things are a little different. Here is the code that I used:
Page Code
<form id="form1" runat="server">
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl.AutoPostBack = true;
rbl.SelectedIndexChanged += rbl_SelectedIndexChanged;
rbl.Items.Add("All");
// generate your dynamic radio buttons here
for (int i = 0; i<5; i++)
{
rbl.Items.Add(string.Format("Dynamic{0}", i));
}
form1.Controls.Add(rbl);
if (!Page.IsPostBack)
{
rbl.SelectedValue = "All";
PopulateTextBox(rbl.SelectedValue);
}
}
void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList foo = (RadioButtonList)sender;
PopulateTextBox(foo.SelectedValue);
}
void PopulateTextBox(string selection)
{
TextBox box = new TextBox();
box.Text = selection;
form1.Controls.Add(box);
}

Respond to Button Events inside Repeater ItemTemplate

so i have removed the datasouce and just have DataBind() then my page is still blowing and refreshing the page and not in a EDIT mode.
what i am trying to do is when the user click on Edit button then make it inline editing the repeater row.
END UPDATE
onItemCommand i have added DataBind()
rpt.DataSource = mydatasource;
rpt.DataBind();
after i do that my page is not in edit mode and it blow away and everyting is refreshed
i have on page_load
if (!IsPostBack)
{
rpt.DataSource = mydatasource;
rpt.DataBind();
}
end update
I've used repeaters many times without problems but something is going on here. I have a repeater and I'm subscribing to the itemDatabound event, But when i click the button (which is a linkbutton inside my repeater itemtemplate) it does not go to the ItemDataBound
<asp:Repeater ID="rpt" runat="server" OnItemCommand="rpt_OnItemCommand" OnItemDataBound="rpt_OnItemDataBound">
<ItemTemplate>
<li>
<asp:Label ID="Label" runat="server" />
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit" CommandArgument='<%# Eval("MyID") %>'
Text='<%# Eval("Title") %>' />
</li>
</ItemTemplate>
</asp:Repeater>
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
}
protected void rpt_OnItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
if (e.Item.ItemIndex == EditIndex)
{
// never come to this line.... after the user click on LinkButton
}
}
}
Don't know if this helps but you must call DataBind() in order for for the OnItemDataBound event to fire. Also my guess is you are trying to set the EditIndex in the OnItemCommand and then use the value in the OnDataBind event. The events fire in the order OnItemDataBound then OnItemCommand so the EditIndex wouldn't be correct anyway in that situation.
Add rpt.DataBind to the OnItemCommand. This workded when I tried it from your code, NOTE that you will be binding twice if you aren't using !IsPostBack for original data bind.
rpt.DataSource = strings;
if (!IsPostBack)
{
rpt.DataBind();
}
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
rpt.DataBind();
}
You must change your rpt_OnItemCommand function.
protected void rpt_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Data.Contacts.RemoveAt(e.Item.ItemIndex);
}
else if (e.CommandName == "edit")
{
EditIndex = e.Item.ItemIndex;
}
else if (e.CommandName == "save")
{
//
}
else if (e.CommandName == "Complete")
{
// your function goes here
}
}
Why do you think that the ItemDataBound is raised when you click your LinkButton? ItemDataBound is only fired when Repeater.DataBind() is called.
Actually the repeater's ItemCommand event is raised instead.
I'm a little confused, but from the example above it looks like you've got it backwards. The button click would never fire the ItemDataBound event. The ItemDataBound event is only called after each item is bound to the repeater.
The button click should fire the ItemCommand event however, and if that's not happening I would check to make sure you've actually assigned the ItemCommand handler, and also make sure that the command name is valid.
On a side note, this behavior can also happen when the repeater is bound at every postback. Make sure that you're binding the repeater when !Page.IsPostBack.

Displaying a default image in gridview

How to display a defualt profile image in gridview if user didn't provided any image.
if (fileUpload.PostedFile == null)
{
lblStatus.Text = "No file specified.";
return;
}
else
{
{
One approach might be to check each row during RowDataBound event to see image exists or not. If it is empty, you can assign a default image url.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
Image image= (Image)e.Row.FindControl("ImageForPerson");
if (image != null && image.ImageIrl == "")
{
image.ImageUrl = // default image url goes here
}
}
}
Do not forget to add RowDataBound event to your GridView definition.
<asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" />
Or If you do not want to use RowDataBound event. At Page_Load you can manually go through each Row of GridView and check ImageUrl one by one.
protected void Page_Load(object sender, EventArgs e)
{
foreach(GridViewRow gvr in GridView1.Rows)
{
Image image = (Image)gvr.FindControl("ImageForPerson");
if (image != null && image.ImageIrl == "")
{
image.ImageUrl = // default image url goes here
}
}
}
This could also be done in a single line in the aspx itself. By using a ternary operator.
<asp:Image ID="Image1" runat="server" ImageUrl='<%# !string.IsNullOrEmpty(Eval("userImage").ToString()) ? "/images/" + Eval("userImage") : "/images/noimage.jpg" %>' />

e.CommandArgument for asp button is not working

I am developing a asp.net application using C#.
I created an .aspx page and placed four buttons on different locations on the page.
On server side, I want to use just one click event for all four buttons.
Here is my code:
aspx page
<asp:Button ID="Button1" runat="server" CommandArgument="Button1" onClick = "allbuttons_Click" />
<asp:Button ID="Button2" runat="server" CommandArgument="Button2" onClick = "allbuttons_Click" />
<asp:Button ID="Button3" runat="server" CommandArgument="Button3" onClick = "allbuttons_Click" />
<asp:Button ID="Button4" runat="server" CommandArgument="Button4" onClick = "allbuttons_Click" />
cs page
protected void allbuttons_Click(object sender, EventArgs e)
{
//Here i want to know which button is pressed
//e.CommandArgument gives an error
}
#Tejs is correct in his comment, looks like you want something like this:
protected void allbuttons_Click(object sender, EventArgs e)
{
var argument = ((Button)sender).CommandArgument;
}
Use
OnCommand =
and
protected void allbuttons_Click(object sender, CommandEventArgs e) { }
Actually you don't need to pass the CommandArgument at all to know which button you pressed. You can get the ID of the button like below:
string id = ((Button)sender).ID;
You can assign command-text to your buttons as follows:
protected void allbuttons_Click(Object sender, CommandEventArgs e) {
switch(e.CommandName) {
case "Button1":
Message.Text = "You clicked the First button";
break;
case "Button2":
Message.Text = "You clicked the Second button";
break;
case "Button3":
Message.Text = "You clicked Third button";
break;
case "Button4":
Message.Text ="You clicked Fourth button";
break;
}
}

Resources