So, I have a Radgrid and multiple controls in it. One of the functionality is to open a PDF once you click on a hyperlink and at the same time insert that clicked value in the database.
I was successful to open a PDF but I am not sure how do I insert this value in the db as I have already used "OnClick" event to open popup window for PDF. Can somebody please guide me in the right way? I have a method with a parameter that inserts the value but how do I call this method when clicked on the hyperlink?
<telerik:GridTemplateColumn UniqueName="GenerateAOC">
<ItemTemplate>
<asp:HyperLink ID="hlGenerateAOC" datanavigateurlfields="ShipmentNumber" Target="AOPLetter" runat="server" DatatextField="ShipmentNumber" Text="GenerateAOC" NavigateUrl='<%# "AdHocAOC.aspx?ShipmentNumber="+Eval("ShipmentNumber")%>' onclick="window.open (this.href, 'AOPLetter', 'height=700,width=700,scrollbars');">
</asp:HyperLink>
</ItemTemplate>
</telerik:GridTemplateColumn>
Try using asp.net LinkButton OnClientClick method to open pdf file and then server side OnCommand handler to save data in database. You can pass arguments e.g. Shipment number of the clicked row using CommandArgument
<asp:LinkButton runat="server" OnClientClick="window.open('<%#AdHocAOC.aspx?ShipmentNumber="+ Eval("ShipmentNumber")%>'); return true;" OnCommand="LinkButton_Command" CommandArgument="<%Eval("ShipmentNumber")%>" CommandName="SavePdfClickDetails" />
On server-side
void LinkButton_Command(Object sender, CommandEventArgs e)
{
//Here you can save data
if(e.CommandName == "SavePdfClickDetails") {
string shipmentNumber = e.CommandArgument;
}
}
Check http://www.dotnetbull.com/2013/05/how-to-handle-click-event-of-linkbutton.html
Related
i'm gonna keep it short and simple
I'm a software engineering student in the 12th grade and as my final project I have decided to make a website. What the website is about doesn't really matter. The problem is this:
In the picture attached there is a textbox inside a templatefield inside that gridview. I need to get the value that the user writes inside. After you write a value you press Purchase. I've looked at similar questions and none offered a working solution. What happens is the value just disappears. I find the right control with FindControl, but the value gets deleted somehow. How do I know I am at the right control? I went to the client side and added to the asp:TextBox the following:
Text="5"
This works perfectly, so I know it gets to the right control but something makes it disappear. My gridview is being populated by a DataSet that is two datasets combined, and I put the Merge command and the DataSource and DataBind are both in if (!this.IsPostBack). I am completely lost and have no idea what to do, Help is much appreciated.The Picture of the Gridview
All the controls in a GridView are accessible by searching the correct row with FindControl. For that you can send the row number as a CommandArgument and use that in code behind. So first start by using OnCommand instead on OnClick and set the CommandArgument on the aspx page.
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Purchase" OnCommand="Button1_Command" CommandArgument='<%# Container.DataItemIndex %>' />
</ItemTemplate>
</asp:TemplateField>
And then in code behind
protected void Button1_Command(object sender, CommandEventArgs e)
{
//get the rownumber from the command argument
int rowIndex = Convert.ToInt32(e.CommandArgument);
//find the textbox in the corrext row with findcontrol
TextBox tb = GridView1.Rows[rowIndex].FindControl("TextBox1") as TextBox;
//get the value from the textbox
try
{
int numberOfTickets = Convert.ToInt32(tb.Text);
}
catch
{
//textbox is empty or not a number
}
}
I Have a page(manage-darkhast-maghale.aspx) that include a gridview and sqldatasourse.one of the gridview columns is a hyperlink that on click redirects users to (pasokhmaghale.aspx?darkhastMaghaleId={0})
in second page there is a formview and sqldatasourse. after double click on update button on edit template on code behind I have typed below codes to redirect user to first page.
protected void UpdateButton_Click(object sender, EventArgs e)
{
Response.Redirect("manage-darkhast-maghale.aspx");
}
but, after running site and clicking on update button only the page begin redirect and the data has no change on gridview and database. anyone can help me please?
Without seeing the code it is very hard to say. From the information you have provided I would say that you need to leave the CommandName attribute on your Update button in the EditItemTemplate i.e.
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update" Text="Update" />
I would add the following event to your FormView that will execute when the record has been updated:
<asp:FormView ID="FormView1" runat="server" OnItemUpdated="FormView1_ItemUpdated"...
In your code behind for the FormView you should have the following:
protected void FormView1_ItemUpdated(object sender, System.Web.UI.WebControls.FormViewUpdatedEventArgs e)
{
if (e.Exception == null && e.AffectedRows > 0)
{
Response.Redirect("manage-darkhast-maghale.aspx");
}
}
I have a grid view in my page in which i added a template feild.inserted a button which upon clicking will redirect the user to the actual request to make edits. The button that i added in the item template is bound to the data.The below is the code for the template feild
<asp:TemplateField HeaderText="RequestId" SortExpression="roc_id">
<EditItemTemplate>
<asp:HyperLink ID="HyperLink1" runat="server" Text='<%# Bind("roc_id") %>'></asp:HyperLink>
</EditItemTemplate>
<ItemTemplate>
<asp:Button ID="Button2" runat="server" onclick="Button2_Click"
Text='<%# Bind("roc_id") %>' />
</ItemTemplate>
</asp:TemplateField>
how do i read the text from the botton click and redirect the user to the request that they click.I have listed the url below and i need to read the requestid at the end of url
protected void Button2_Click(object sender, EventArgs e)
{
Response.Redirect("http://xxxxxxxxxxxxxxxxxxxxxxxxxxx/Edit.asp?ROCID=
}
Thanks
You can use GridView.RowCommand to capture the button click event.
Also, you need to add a command name to the button in template field.
There is a good article about Respond to Button Events in a GridView Control which will help you
I have a question regarding passing Session Variables to a text-box in an Update Panel (which is displayed in a Modal PopUp).
This is the code I have so far:
ASPX CODE:
<asp:TemplateField HeaderText="Link">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" Text="Link" runat="server" OnClick="LinkButton1_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" OnLoad="panel_Load">
<ContentTemplate>
<asp:Button ID="OKButton" runat="server" Text="Close" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
<asp:ModalPopupExtender ID="mpe" runat="server" TargetControlID="ClientButton" PopupControlID="UpdatePanel1" OkControlID="OKButton">
</asp:ModalPopupExtender>
<asp:Button ID="ClientButton" runat="server" Text="Launch Modal Popup (Client)" style="display:none;" />
CODE BEHIND (C#):
protected void LinkButton1_Click(object sender, EventArgs e)
{
GridViewRow clickedRow = ((LinkButton)sender).NamingContainer as GridViewRow;
Label lbl_nme = (Label)clickedRow.FindControl("lbl_name");
String string_nme = lbl_nme.Text.ToString();
Session["Name"] = string_nme;
mpe.Show();
}
protected void panel_Load(object sender, EventArgs e)
{
Label1.Text = (string)(Session["Name"]);
}
So basically I have a GridView with name, address etc… When the user clicks on a link in a row, then the value for the name field of that row is saved as a session variable. Then a Modal PopUp is displayed. The Modal PopUp should then show the Name which was saved as a Session variable.
The code sort of works. What I’m experiencing is that when I click a row, the Label1.Text in the Modal PopUp is empty. So if I close the PopUp then click another link in another row, the PopUp then displays the Name of the row that was clicked previously.
In other words.. If row 1 has Name “Kevin” and row 2 has Name “Nathaniel”, and I click a link to open the Modal PopUp of row 1, I would expect the PopUp to display “Kevin”. But it doesn’t. The first time I click a link after rebuilding the application, nothing is displayed. But say I click row 2 after clicking row1, then the Modal PopUp displays the value of the row I clicked before, i.e. “Kevin” when I expect it to be “Nathaniel”.
I hope I didn’t confuse anyone. I’m a newbie and I’m just getting into this stuff, so I’d appreciate it if someone could help me out, preferably with examples of code etc.
Thank you. Much appreciated.
The "Load" event (panel_Load) occurs before the "Click" event (LinkButton1_Click) so it only sees the previous value.
The quick fix is to set the label in the "Click" event as well. Unless ViewState is enabled for the label (ick!) the label may have to be [re]set in the "Load" as well, depending upon when/how updates occur.
See ASP.NET Page Life Cycle Overview and ASP.NET Application and Page Life Cycle: Page Events.
Happy coding.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling a functon in code behind by using an imagebutton in a gridview
I have an ImageButton within a gridview in .aspx on clicking this imagebutton i have to call a function.
This is how i tried and the function was not being called.
Code inside.aspx page:
<GridView ......>
<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# DataBinder.Eval(Container.DataItem,"VehID","mngVeh.aspx?delid={0}") %>'>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/images/delete.jpeg" width='24' height='24'
OnClick="DeleteUrlImageButton_Click"
OnClientClick="return confirm('Are you sure you want to delete?');" />
</asp:HyperLink>
</GridView>
code in .aspx.cs page:
public void DeleteUrlImageButton_Click(object sender, EventArgs e)
{
//code to perform the necessary action.
}
I think you're mixing controls up here.
I would use an ImageUrl on the asp:hyperlink itself, and also the OnClick property. You can handle the OnClientClick by using javascript inside the NavigateUrl property.
I don't see a reason for having a nested ImageButton - I doubt this even works.
Or, just remove the asp:hyperlink completely and do a redirect inside the OnClick event.
The problem here is twofold: First, the click event does not bubble up through the GridView, which is why the event is not firing. Second, that there will be an ImageButton for each row of the GridView, and you need to identify which row the event originated from (or more accurately, which underlying datasource record that row corresponds to).
A common way to approach this problem is to use the ImageButton.CommandName and ImageButton.CommandArgument properties, and handle the GridView.RowCommand event.
Markup:
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton runat="server" ID="DeleteUrlImageButton" ImageUrl="~/images/delete.jpeg"
Width='24' Height='24' OnClientClick="return confirm('Are you sure you want to delete?');"
CommandName="MyDelete" CommandArgument='<%# Eval("RecordId") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if ((e.CommandName == "MyDelete") && (e.CommandArgument != null))
{
//Add delete code here using e.CommandArgument to identify the correct record.
// For instance:
MyDataObject obj = new MyDataObject();
obj.RecordId = int.Parse(e.CommandArgument.ToString());
MyDataObject.Delete(obj);
}
}
Edit: I see you provided the name of the ID field in your example, so you would probably want to replace "RecordId" in my example with "VehID".