LinkButton in Repeater Command does not fire on click - asp.net

I have a linkbutton in a Repeater that needs to fire a method in the codebehind when clicked, but the method never executes when the LinkButton is clicked. Here is the HTML for the Repeater:
<asp:Repeater ID="rptFeatures" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("Name") %>'
CommandName="listItem_Click"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "AccessListItemId") %>'>
</asp:LinkButton><br />
</ItemTemplate>
</asp:Repeater>
Here is the method that needs to fire onClick:
protected void listItem_Click(object sender, RepeaterCommandEventArgs e)
{
throw new NotImplementedException();
}
The method never gets called as I have a breakpoint on the NotEmplementedException just to see if it hits. Can someone please tell me what I am doing wrong here?
Any assistance is greatly appreciated!

Try This :
<asp:Repeater ID="rptFeatures" runat="server" OnItemCommand="rptFeatures_OnItemCommand">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("Name") %>'
CommandName="listClick"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "AccessListItemId") %>'>
</asp:LinkButton><br />
</ItemTemplate>
</asp:Repeater>
protected void rptFeatures_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("listItem_Click"))
{
// your code
}
}

Instead of CommandName Use onClick Event.
OnClick="listItem_Click"

I needed to add "OnItemCommand=listItem_ItemCommand" to the Repeater to Baseer Haider's answer. The repeater now looks like this:
<asp:Repeater ID="rptFeatures" runat="server" OnItemCommand="listItem_ItemCommand">
<ItemTemplate>
<asp:LinkButton runat="server" Text='<%# Eval("Name") %>' CommandName="listClick" CommandArgument='<%# DataBinder.Eval(Container.DataItem, "AccessListItemId") %>'></asp:LinkButton><br />
</ItemTemplate>
</asp:Repeater>
Now it hits the method with arguments included:
protected void listItem_ItemCommand (Object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("listItem_Click"))
{
// your code
}
}

Related

Oncommand event is not working for link button in list view

i am using image button and link button in list view and binding data in onload method.oncommand for image button is working fine but oncommand for link button is not firing here is code of list view:
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ListView runat="server" ID="List1" >
<ItemTemplate>
<table>
<tr>
<td>
<asp:ImageButton runat="server" ID="imagebutton" ImageUrl='<%# Eval("image") %>' CommandArgument='<%# Eval("id") %>' CommandName="button" Width="100px" Height="100px" OnCommand="imagebutton_Command" ></asp:ImageButton>
</td>
<td>
<asp:LinkButton ID="title" runat="server" Text='<%# Eval("title") %>' CommandArgument='<%# Eval("id") %>' CommandName="link" OnCommand="imagebutton_Command"></asp:LinkButton></td>
</tr>
</table>
</ItemTemplate>
</asp:ListView>
</asp:Content>
and code for oncommad event is:
protected void imagebutton_Command(object sender, CommandEventArgs e)
{
try
{
Response.Redirect("Welcome.aspx?url=" + e.CommandArgument.ToString());
}
catch (Exception ex)
{
ex.ToString();
}
}
I also tried break point on oncommand method but when i click on image button it works fine but when i click on link button it does not fired and does not come in oncommad block

asp.net editable gridview "no data found" row causes issue after postback

I have a simple problem with my gridview. It is an editable gridview which allows inserting, updating and deleting rows. It is bound to a datatable. I store the datatable in the viewstate and bind the datatable on page_load for every postback. There is no column order or paging and I only have a few records on this gridview. When there is no data to display, I add an empty message text like "no data found" manually to grid. It is okey for the first page_load. But after postback, this text disappears, but the row is still there. And the main problem is the row has "edit" and "delete" command columns after postback. This row should not have edit or delete columns after postback. It works really fine before postback.
This problem never happens if there is at least one row in gridview.
If I have at least one row, then if I add more rows: No problem
If I have the empty grid, then I add a row (before any postback for example combobox value change): No problem
If I have the empty grid: then I do a postback by changing a value in a combobox, then "no rows found" message in the grid disappearing and there is an extra row in the grid which has no text and has edit and delete columns
If I have the grid with that unwanted row, then I add a row to that gridview: The unwanted row is going away and new row appearing on the grid. No problem
So the only problem is that extra row which is appearing after postback.
Code details are below. Please help me with this silly problem. I couldnt solve it for days.
ASPX:
<asp:GridView ID="grdTerminals" runat="server"
AutoGenerateColumns="False" DataKeyNames="TRM_ID"
OnRowCancelingEdit="grdTerminals_RowCancelingEdit"
OnRowDataBound="grdTerminals_RowDataBound"
OnRowEditing="grdTerminals_RowEditing"
OnRowUpdating="grdTerminals_RowUpdating" ShowFooter="True"
OnRowCommand="grdTerminals_RowCommand"
OnRowDeleting="grdTerminals_RowDeleting"
HeaderStyle-BackColor="#73be1e" HeaderStyle-ForeColor="Window" HeaderStyle-Font-Bold="true" Width="500px" HeaderStyle-Height="30">
<Columns>
<asp:TemplateField HeaderText="Terminal" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:DropDownList ID="ddlTerminal" runat="server" DataTextField="TRM_MNMC" DataValueField="TRM_ID">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lblTerminal" runat="server" Text='<%# Eval("TRM_MNMC") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:DropDownList ID="ddlNewTerminal" runat="server" DataTextField="TRM_MNMC" DataValueField="TRM_ID">
</asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TAS No" HeaderStyle-HorizontalAlign="Left">
<EditItemTemplate>
<asp:TextBox ID="txtTASNo" runat="server" Text='<%# Bind("TAS_NO") %>' Width="120"></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNewTASNo" runat="server" Width="120"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblTASNo" runat="server" Text='<%# Bind("TAS_NO") %>' Width="120"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Kaydet"></asp:LinkButton>
<asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert" Text="Insert"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
<asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="" ShowDeleteButton="True" ShowHeader="True" DeleteText="Delete"/>
</Columns>
</asp:GridView>
C#
// I am storing the data in ViewState
DataTable dtTerminals
{
get
{
return ViewState["_dtVehicleTerminals"] as DataTable;
}
set
{
ViewState["_dtVehicleTerminals"] = value;
}
}
//Page Load
protected void Page_Load(object sender, EventArgs e)
{
if (dtTerminals == null)
{
dtTerminals = new DataTable();
dtTerminals.Columns.Add("TRM_ID");
dtTerminals.Columns.Add("TRM_MNMC");
dtTerminals.Columns.Add("TAS_NO");
}
if (!IsPostBack)
{
string VhcId = Request["vhc_id"];//Edit mode
if (!string.IsNullOrEmpty(VhcId))
{
BindTerminalInfo(VhcId);
}
else
{
//To show empty grid
BindTerminalGrid();
}
}
// Bind Terminal Info
void BindTerminalInfo(string VhcId)
{
dtTerminals = VehicleManager.GetVehicleTerminals(VhcId);
BindTerminalGrid();
}
//Bind Terminal Grid
public void BindTerminalGrid()
{
if (dtTerminals.Rows.Count > 0)
{
grdTerminals.DataSource = dtTerminals;
grdTerminals.DataBind();
}
else
{
//Show No Records Found
dtTerminals.Rows.Add(dtTerminals.NewRow());
grdTerminals.DataSource = dtTerminals;
grdTerminals.DataBind();
grdTerminals.Rows[0].Cells.Clear();
grdTerminals.Rows[0].Cells.Add(new TableCell());
grdTerminals.Rows[0].Cells[0].Text = "No Data found";
dtTerminals.Rows.Clear();
}
}
//Other methods related to grid functionality
protected void grdTerminals_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlTerminal = (DropDownList)e.Row.FindControl("ddlTerminal");
if (ddlTerminal != null)
{
ddlTerminal.DataSource = VehicleManager.GetDefaultUserTerminalNames();
ddlTerminal.DataBind();
ddlTerminal.DataTextField = "TRM_MNMC";
ddlTerminal.DataValueField = "TRM_ID";
ddlTerminal.SelectedValue = grdTerminals.DataKeys[e.Row.RowIndex].Values[0].ToString();
}
}
if (e.Row.RowType == DataControlRowType.Footer)
{
DropDownList ddlNewTerminal = (DropDownList)e.Row.FindControl("ddlNewTerminal");
ddlNewTerminal.DataSource = VehicleManager.GetDefaultUserTerminalNames();
ddlNewTerminal.DataBind();
}
}
protected void grdTerminals_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
grdTerminals.EditIndex = -1;
BindTerminalGrid();
}
protected void grdTerminals_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblId = (Label)grdTerminals.Rows[e.RowIndex].FindControl("lblTrmUnqId");
DropDownList ddlTerminal = (DropDownList)grdTerminals.Rows[e.RowIndex].FindControl("ddlTerminal");
TextBox txtTASNo = (TextBox)grdTerminals.Rows[e.RowIndex].FindControl("txtTASNo");
dtTerminals.Rows[e.RowIndex]["TRM_ID"] = ddlTerminal.SelectedValue;
dtTerminals.Rows[e.RowIndex]["TRM_MNMC"] = ddlTerminal.SelectedItem;
dtTerminals.Rows[e.RowIndex]["TAS_NO"] = txtTASNo.Text;
grdTerminals.EditIndex = -1;
BindTerminalGrid();
}
protected void grdTerminals_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string TrmId = Convert.ToString(grdTerminals.DataKeys[e.RowIndex].Values[0]);
dtTerminals.Rows.Remove(dtTerminals.Rows[e.RowIndex]);
BindTerminalGrid();
}
protected void grdTerminals_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("Insert"))
{
DropDownList ddlNewTerminal = (DropDownList)grdTerminals.FooterRow.FindControl("ddlNewTerminal");
TextBox txtTASNo = (TextBox)grdTerminals.FooterRow.FindControl("txtNewTASNo");
DataRow dr = dtTerminals.NewRow();
dr["TRM_ID"] = ddlNewTerminal.SelectedValue;
dr["TRM_MNMC"] = ddlNewTerminal.SelectedItem.Text;
dr["TAS_NO"] = txtTASNo.Text;
dtTerminals.Rows.Add(dr);
BindTerminalGrid();
}
}
protected void grdTerminals_RowEditing(object sender, GridViewEditEventArgs e)
{
grdTerminals.EditIndex = e.NewEditIndex;
BindTerminalGrid();
}
I am not sure it is the correct solution . But try adding Empty data text property of Gridview.
<asp:gridview id="CustomersGridView"
datasourceid="CustomersSqlDataSource"
autogeneratecolumns="true"
emptydatatext="No data in the data source."
runat="server">
<emptydatarowstyle backcolor="LightBlue"
forecolor="Red"/>
</asp:gridview>
Or try like this
<asp:TemplateField>
<EditItemTemplate>
<asp:ImageButton ID="imgBtnUpdate" runat="server" Height="32px"
ImageUrl="~/Images/Update.jpg" ToolTip="Update" Width="32px" CommandName="Update"/>
<asp:ImageButton ID="imgBtnDelete" runat="server" Height="32px"
ImageUrl="~/Images/delete.gif" style="margin-left: 0px" ToolTip="Delete"
Width="32px" CommandName="Delete"/>
<asp:ImageButton ID="imgBtnCancel" runat="server" Height="32px"
ImageUrl="~/Images/Cancel.png" ToolTip="Cancel" Width="32px" CommandName="Cancel"/>
</EditItemTemplate>
<FooterTemplate>
<asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/AddNew.gif"
ToolTip="Add New Record" CommandName="Add"/>
</FooterTemplate>
<ItemTemplate>
<asp:ImageButton ID="imgBtnEdit" runat="server" Height="32px"
ImageUrl="~/Images/pencil.png" ToolTip="Edit" Width="32px" CommandName="Edit"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<EditItemTemplate>
<asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtNameFooter" runat="server"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
</ItemTemplate>
Finally I have fixed this problem. The solution is adding
if (!Request.Form["__EVENTTARGET"].Split('$').Contains(grdTerminals.ID))
{
BindTerminalGrid();
}
code block to If(Postback) statement. I should rebind data for everypostback but only if the control which caused postback is not the "datagrid".

Repeater inside a ListView: how to bind a command and call it ?

Scenario: I've a Repeater inside a ListView.
Inside the repeater i've a LinkButton; This is the syntax
<asp:LinkButton runat="server" ID="lnkRemoveComment" Text="Delete" Visible='<%# CheckVisibility() %>' CommandName="DeleteComment" CommandArgument='<%# Eval("id")%>'> </asp:LinkButton>
I don't know the syntax, in server side code, to call:
DeleteComment
CheckVisibility
I don't know because repeater is nested in an ListView.
And inserting code inside ListView ItemCommand not works!
Thanks
Example :
.aspx
------------------
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnCommand="LinkButton1_Command" CommandName="MyUpdate" CommandArgument='<%# Eval("erid") %>'>LinkButton</asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
.cs
------------------
protected void LinkButton1_Command(object sender, CommandEventArgs e)
{
if (e.CommandName == "MyUpdate"){
//e.CommandArgument --> contain the erid value
//Do something
}
}

Asp.Net set Visibility of Dropdown list inside gridview in aTemplate Field

I have a Gridview with Template field
<asp:TemplateField HeaderText="Scoring">
<ItemTemplate>
<asp:DropDownList ID="ddlY_N_NA" runat="server"
Visible='<%#Eval("IsTextBox")%>' ></asp:DropDownList>
<asp:TextBox ID="txtAudit" runat="server"
Visible='<%#Eval("IsTextBox")%>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
I need to set the visibility of drop down based on the visibility of Textbox.
Either of the two has to be shown per row but not both.
Can any help me in this.
Thanks in advance.
Try the following:
<asp:TemplateField HeaderText="Scoring">
<ItemTemplate>
<asp:DropDownList ID="ddlY_N_NA" runat="server"
Visible='<%# ((bool)Eval("IsTextBox")) ? "false" : "true" %>' >
</asp:DropDownList>
<asp:TextBox ID="txtAudit" runat="server"
Visible='<%#Eval("IsTextBox")%>' ></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
You can do it with the RowDataBound event of the Gridview.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow dr = ((DataRowView)e.Row.DataItem).Row;
if (dr["ColumnName"].ToString()) // your Condition
{
((DropDownList)e.Row.FindControl("dropdownID")).Visible = false;
}
else if (dr["ColumnName"].ToString()) // your Condition
{
((TextBox)e.Row.FindControl("TextboxID")).Visible = false;
}
}
}
Hi, Akram Shahda below is my solution
<asp:TemplateField HeaderText="Scoring" HeaderStyle-Width="12%">
<ItemTemplate>
<asp:DropDownList ID="ddlY_N_NA" Visible='<%#SetVisibility(DataBinder.Eval(Container.DataItem,"IsTextBox"))%>'
runat="server" CssClass="Qdropdown">
</asp:DropDownList>
<asp:TextBox onkeypress="return isNumberKey(event);" ID="txtAudit" Visible='<%#Convert.ToBoolean(Eval("IsTextBox"))%>'
MaxLength="10" runat="server" CssClass="Qinputbox" Width="54px" ValidationGroup="txt"></asp:TextBox>
<asp:HiddenField ID="hdnTextBoxCondition" Value='<%#SetTextBoxVisibility(Eval("IsTextBox"),Eval("TextBoxConditions"))%>'
Visible='<%#Eval("IsTextBox")%>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
In the code behind I have written the methods which will set the visibility
public bool SetVisibility(object value)
{
if (Convert.ToBoolean(value))
return false;
else
return true;
}
public string SetTextBoxVisibility(object value, object condition)
{
if (Convert.ToBoolean(value))
return Convert.ToString(condition);
else
return "";
}

How to assign a value to a bind() in asp.net <inserttemplate>

<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductId") %>'>
</asp:TextBox>
</InsertItemTemplate>
How do I pass a value to the Bind("ProductId")? More specifically:
Request.QueryString["ProductId"]
EDIT:
leave it with your original bind but add OnDataBinding
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("ProductId") %>'
OnDataBinding = "TextBox1_OnDataBinding"></asp:TextBox>
and
protected void TextBox1_OnDataBinding(object sender, EventArgs e)
{
(sender as TextBox).Text = Request.QueryString["ProductId"];
}
In addition to
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Request.QueryString["ProductID"]%>' />
You need to handle the OnInserting event of your datasource and set the value there with the querystring parameter.
You will have to explicitly set the TextBox value in the DataBound event of your DataBoundControl (repeater/gv etc).

Resources