I have a LinkButton inside of a telerik grid when it is clicked it updates the database. The trouble comes at the first line where I recieve an error that I am unable to cast the radgrid to linkButton. Can someone shed a little light. Here is the Error Msg.
Telerik.Web.UI.RadGrid' to type 'System.Web.UI.WebControls.LinkButton
Here is my method:
protected void rad_grdCompleteRequest(object sender, EventArgs e)
{
LinkButton btnCompleteRequest = (LinkButton)sender;
int requestID = Convert.ToInt32(btnCompleteRequest.Attributes["RequestID"]);
SqlManager.UpdateRequest(requestID, 3);
Response.Redirect(Request.RawUrl);
}
Please try with the below code snippet.
.aspx
// Normal Mode
<telerik:GridTemplateColumn>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
</telerik:GridTemplateColumn>
// Edit Mode
<telerik:GridTemplateColumn>
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">LinkButton</asp:LinkButton>
</EditItemTemplate>
</telerik:GridTemplateColumn>
.aspx.cs
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton LinkButton1 = sender as LinkButton;
// Do your logic here
}
Please let me know if you have any concerns.
Related
I have hyperlink and a hidden field inside a datalist as shown below
<asp:DataList ID="clientsList" runat="server" OnSelectedIndexChanged="clientsList_SelectedIndexChanged1" >
<ItemTemplate>
<asp:HyperLink ID="hlName" runat="server" Text='<%# Bind("Name") %>' NavigateUrl="#" ></asp:HyperLink>
<asp:HiddenField ID="HiddenFieldID" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
</asp:DataList>
When user clicks on any hyperlink, I need to store that value in an application variable. But the SelectedIndexChange event never fires.
This is my code:
protected void clientsList_SelectedIndexChanged1(object sender, EventArgs e)
{
int idx = clientsList.SelectedIndex;
HiddenField hiddenCID = clientsList.Items[idx].FindControl("HiddenFieldID") as HiddenField;
if (hiddenCID != null)
{
Logger.UpdateLog("Selected ID: " + hiddenCID.Value.ToString());
}
}
I am binding data from database to the datalist in the Page_load event as below and this is working fine.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
Any idea why this event is never fired?
Also is my code is right as far as accessing the hiddenfield value is concerned?
Have you set commandname property of hyperlink field?
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">Select</asp:LinkButton>
Now in selectedindexchanged method:
protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
{
int idx = DataList1.SelectedIndex;
Label lbl = (Label)DataList1.Items[idx].FindControl("Label1");
int id =Convert.ToInt32(DataList1.SelectedValue);
}
Does it work?
I am having GridView control inside Wizard control, and I am having a linkbutton inside grid, clicking on which will change active index of wizard.
I have three GridViewControls and I am using same Event for RowCommand of these Grids, but its not working, I tried applying breakpoint but its not hitting the break point.
This is my code
w ID="GVUsers" runat="server" OnRowDataBound="GVUsers_RowDataBound" OnRowCommand="GVUsers_RowCommand"
OnRowDeleting="GVUsers_RowDeleting" AutoGenerateColumns="false" CssClass="table">
<Columns>
<asp:TemplateField HeaderText="Crimes" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:Label ID="Username" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Username") %>'></asp:Label>
<asp:Label ID="gender" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "Gender") %>'></asp:Label>
<asp:Panel ID="divmsg" runat="server">
<asp:LinkButton
ID="btnlnkpg18" runat="server" Text="Click here" CommandName="pg18"></asp:LinkButton>
</asp:Panel>
</ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HeaderText="Delete" ItemStyle-HorizontalAlign="Left">
<ItemTemplate>
<asp:LinkButton ID="delbtn" runat="server" Text="Delete" OnClientClick="return confirm('Do you really want to delete?');"
CommandName="delete" CommandArgument='<%#DataBinder.Eval(Container.DataItem, "UserId") %>'
CssClass="DeleteBtn"></asp:LinkButton>
</ItemTemplate>
<asp:TemplateField>
</Columns>
</asp:GridView>
protected void GVUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataRow row = ((DataRowView)e.Row.DataItem).Row;
var lblgender= e.Row.FindControl("gender") as Label;
var divlnk=e.Row.FindControl("divmsg") as Panel;
if(lblgender.Text=="M")
divlink.Visible=true;
else
divlink.Visible=false;
}
}
protected void GVUsers_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "delete")
{
//Delete
}
if (e.CommandName == "pg18")
{
Wizard1.ActiveStepIndex = 16;
}
}
I also tried setting CommandName in RowDataBound but no luck, Also the Delete Button is not working.
I am databinding GridView like this
if(!Page.IsPostBack)
{
//Bind GridView
}
Syed, try doing this:
1) Rename your RowDataBound event to this:
protected void temp()
2) Go to the aspx page and delete the RowDataBound property from the gridview.
3) With the gridview still highlighted, click design view.
4) In properties click on 'Events' (the lightning bolt).
4) Double click inside the box for RowDatabound to create the event handler in the .cs
5) With in the RowDatabound inside the .cs add a reference to temp();
6) Add a breakpoint just about the temp() within RowDatabound and run your code.
See if it hits the code, if it does then copy everything from test into the RowDataBound.
<asp:ListView runat="server" ID="lvAttachments" ItemPlaceholderID="ph" OnItemDataBound="lvAttachments_ItemDataBound">
<LayoutTemplate>
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
</LayoutTemplate>
<ItemTemplate>
<asp:LinkButton runat="server" ID="btnReject" OnClick="btnReject_Click"></asp:LinkButton>
<asp:TextBox runat="server" ID="tbReason" CssClass="textbox" TextMode="MultiLine"></asp:TextBox>
</ItemTemplate>
</asp:ListView>
My question is: how to get text from textbox, while btnReject click action?
protected void btnReject_Click(object sender, EventArgs e)
{
LinkButton btnReject = (LinkButton)sender;
// how to get tbReason.Text from this item?
}
Regards
edit:
Problem resolved !
We just need to add in Page_Load code to prevent re-load listview and clear textboxes:)
http://forums.asp.net/t/1712482.aspx/1
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
lvAttachments.DataSource = tAttachmentBO.getAttachmentsToAccept();
lvAttachments.DataBind();
}
}
Something like this should work
I'm on ipad so apologize for any mistakes
TextBox txt = (TextBox)btnReject.Parent.FindControl("tbReason")
I have a GridView in which I have put an extra column Details, and in each row of the GridView, I have a LinkButton called Details. So when I click this I want an event to trigger.
The asp.net code is:
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="Details" runat="server" Text="Details"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Kindly help. Thank you.
Then use the LinkButton.Click-event:
<asp:LinkButton ID="Details" OnClick="LinkClicked" runat="server" Text="Details">
in codebehind:
protected void LinkClicked(Object sender, EventArgs e)
{
LinkButton link = (LinkButton)sender;
GridViewRow row = (GridViewRow)link.NamingContainer;
// assuming there's a label with ID=Label1 in another TemplateField:
Label label1 = (Label)row.FindControl("Label1");
label1.Text="You clicked the link button";
}
If you need the GridViewRow of the GridView where the user clicked the link, for example to find controls in other templateFields, you can use the NamingContainerproperty.
Handle GridView.RowCommand Event
Occurs when a button is clicked in a GridView control.
<asp:LinkButton ID="Details" commandname="Details" runat="server" Text="Details"></asp:LinkButton>
void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked.
if(e.CommandName=="Details")
{
// Convert the row index stored in the CommandArgument
// property to an Integer.
int index = Convert.ToInt32(e.CommandArgument);
//Your Code
}
}
You can have a command argument to determine which row was clicked (assuming you have more than one button (standard buttons/link buttons) in a row
<asp:GridView OnRowCommand="GridViewOnItemCommand" runat="server">
<asp:TemplateField HeaderText="Details">
<ItemTemplate>
<asp:LinkButton ID="btnDetails" CommandName="Details" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Details" runat="server"/>
<asp:LinkButton ID="btnDelete" CommandName="Delete" CommandArgument="YOUR_COMMAND_ARG_HERE" Text="Delete" runat="server"/>
<ItemTemplate>
</asp:TemplateField>
</asp:GridView>
in the code behind file
protected void GridViewOnItemCommand(object sender, GridViewCommandEventArgs e)
{
//you can determine which button was clicked (detail or delete)
var command = e.CommandName;
//you can determine which row was clicked
var arg = e.CommandArgument;
if(command == "Details")
ShowDetails(arg);
if(command == "Delete")
Delete(arg);
}
hope this helps
I stripped this example to make it simple. I have a gridview with a template field. The template field contains two buttons and a label (They must be in the same template field). I want the first button to set the label text to "Win", and the other button to set the label text to "fail". The onrowcommand doesnt seem to be triggered by buttons in a template field. How can I accomplish this?
My gridview code is below:
<asp:GridView ID="GridView1" runat="server" EnableModelValidation="True" AutoGenerateColumns="False"
OnRowCommand="Gridview1_RowCommand">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="btnWin" runat="server" CommandName="Win" Text="Win" />
<asp:Button ID="btnFail" runat="server" CommandName="Fail" Text="Fail" />
<asp:Label ID="lblStatus" runat="server" Text='<%# Bind("text") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my code behind:
protected void Page_Load(object sender, EventArgs e)
{
DataTable myTable = new DataTable();
myTable.Columns.Add("text", typeof(string));
myTable.Rows.Add("First Entry");
myTable.Rows.Add("Second Entry");
GridView1.DataSource = myTable;
GridView1.DataBind();
}
public void Gridview1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
//set lblStatus.text to "Win", or "Fail"
}
Thanks in advance!
Here you go...
public void Gridview1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
lblStatus.Text = e.CommandName;
}
I see that there is more to this question than is answered here, bear with me. One way would be to delegate the OnCommand event of each button to its designated event handler, as follows:
<div>
<asp:GridView ID="MyGridView" runat="server" EnableModelValidation="true" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="MyWinButton" runat="server" OnCommand="MyWinButton_OnCommand" CommandName="Win" Text="Win" />
<asp:Label ID="MyStatusLabel" runat="server" Text='<%# Bind("text") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
public void MyWinButton_OnCommand(Object sender, CommandEventArgs e)
{
var label = ((Button)sender).Parent.FindControl("MyStatusLabel") as Label;
label.Text = e.CommandName;
}
Also, as Alison suggests, you won't see the desired output of this unless you use !IsPostBack in Page_Load. Furthermore, on doing so this does in fact enable you to use the one row command event handler as initially suggested, albeit with a slight change in the label retrieval:
public void MyGridView_OnRowCommand(Object sender, GridViewCommandEventArgs e)
{
var label = ((Button)e.CommandSource).Parent.FindControl("MyStatusLabel") as Label;
label.Text = e.CommandName;
}
Are you using a MasterPage with ViewState turned off? Buttons in a template field with ViewState set to false will not fire.
Also, you should change your Page_Load to NOT rebind on postback"
protected void Page_Load(object sender, EventArgs e)
{
if (!page.IsPostBack) {
DataTable myTable = new DataTable();
myTable.Columns.Add("text", typeof(string));
myTable.Rows.Add("First Entry");
myTable.Rows.Add("Second Entry");
GridView1.DataSource = myTable;
GridView1.DataBind();
}
}
The rebinding may be interfering.