Is there a way to use a query string or some javascript to collect a value from a gridview's button-selected row's specific column, open another form as a popup, and have one of that form's textboxes be populated with that value?
Use the query string method to pass the value of selected from the gridview. link should be like popup.aspx?selectedvalue=Gridview.SelectedItem. In the popup screen code should access the value by using Request.QueryString["selectedvalue"] method. Even you
can have a session object to pass it. But I prefer query string. Happy coding
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.txtTransactionNumber.Text = Request.QueryString("Number")
End Sub
But I'm not sure how to get the Number from my gridview.... thanks for any additional help!
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs)
Response.Redirect("RenewalsDetails.aspx?Number=" + Me.Gridview1. .......... )
End Sub
You pass the values from one page to another page using querstring right,here through query string the value is passed to targer page in target page load event you can retieve the value and its passed to u r textbox.
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
DirectCast(e.Row.FindControl("btnSelect"), Button).Attributes.Add _
("onclick", "javascript:GetRowValue('" & e.Row.Cells(1).Text & "')")
End If
End Sub
Private Sub btnSubmit_Click(sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "window.open('RenewalsDetails.aspx?Number=" + e.Row.Cells(1).Text + "','Report','width=900,height=500,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no'); return false;")
End If
End Sub
Several responses voted for passing the query string value to the popup page and I agree. Also, sudheerkumar is on the right track to inject javascript into the page. However, it seems like you might be overthinking this a little.
You have a gridview with some data and each row has a button. When the button is clicked, you want a separate window to pop up with details in it that are based on the gridview row that was clicked. Right, so far? Here's the easy way to do that:
If you haven't already done so, use the smart tag wizard to convert the gridview's command field to a template field. You will then be able to access the mark up for the button. Notice that the button has an attribute called CommandName. This is the action
that will be performed when the button is clicked. Yours might say CommandName="Select" or something similar. There are several pre-baked command names like Select, Edit, Update, Cancel, Insert, etc. However, you can change those so that the command name
does what you want it to do. Start off by changing your CommandName to something like ViewDetails. CommandName="ViewDetails"
Next, your code-behind needs the ID value or whatever your popup page will use in the query string. Another attribute of the button is CommandArgument. Use a server tag to pass the data key that you want in your query string. For example:
This button has the CommandName of ViewDetails and it takes in the CommandArgument of the ID for that row. (The ID does not have to actually be displayed to your users)
Next, go to your code-behind and use the dropdown lists to create the gridview's RowCommand event. Inside the RowCommand event, check the CommandName being passed in and inject the javascript to open the popup accordingly. This code should get you pretty close:
If e.CommandName = "ViewDetails" Then
Dim str As String = ("<script language='javascript'>" & "window.open('YourPageName.aspx?cn=") + e.CommandArgument & "', 'PopUp'," & "'top=0, left=0, width=500, height=500, menubar=0,toolbar=0,status=0,resizable=yes')"
If Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "PopUp") Then
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "PopUp", str)
End If
End If
Notice that the ID from the html mark up is being passed in through CommandArgument and the code above is using that value in e.CommandArgument.
That's it. Click the button for any row in the gridview and the corresponding record should pop up in a separate window.
Now... if your page has an UpdatePanel (which I hate) you'll need to change the last two lines about the Client Script to this:
We're close, it's making more sense, but I'm missing something, but let's not go there :)
Anyway, here is my markup and code behind as it stands (I included my datasource):
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.txtTransactionNumber.Text = Request.QueryString("Renewal_ID_Rec")
End Sub
</script>
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If e.CommandName = "ViewDetails" Then
Dim str As String = ("<script language='javascript'>" & "window.open('RenewalsDetails.aspx?=") + e.CommandArgument & "', 'PopUp'," & "'top=0, left=100, width=800, height=1000, menubar=0,toolbar=0,status=0,resizable=no')"
If Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "PopUp") Then
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "PopUp", str)
End If
End If
End Sub
We're close, it's making more sense, but I'm missing something, but let's not go there :)
Anyway, here is my markup and code behind as it stands (I included my datasource):
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.txtTransactionNumber.Text = Request.QueryString("Renewal_ID_Rec")
End Sub
</script>
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If e.CommandName = "ViewDetails" Then
Dim str As String = ("<script language='javascript'>" & "window.open('RenewalsDetails.aspx?=") + e.CommandArgument & "', 'PopUp'," & "'top=0, left=100, width=800, height=1000, menubar=0,toolbar=0,status=0,resizable=no')"
If Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "PopUp") Then
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "PopUp", str)
End If
End If
End Sub
We're close, Eugene. The preformatted string to inject as javascript is probably where the problem lies and that may be partly my fault as it has been a few years since I worked with VB so the formatting wasn't right on the first pass. Right now, you have:
Dim str As String = ("<script language='javascript'>" & "window.open('RenewalsDetails.aspx?=") + e.CommandArgument & "', 'PopUp'," & "'top=0, left=100, width=800, height=1000, menubar=0,toolbar=0,status=0,resizable=no')"
The compiler won't be able to make sense of that since the opening parenthesis is outside of the quote mark (and unneeded) and the closing one is inside the quote mark (and also unneeded). Also, the query string name is missing from the url. Let's clean
it up a little. Try this, instead:
Dim str As String = "<script language='javascript'>window.open('RenewalsDetails.aspx?Renewal_ID_Rec=" & e.CommandArgument & "', 'PopUp', 'top=0, left=100, width=800, height=1000, menubar=0,toolbar=0,status=0,resizable=no'"
The VB compiler had no issue with the changes and they make sense, but I'm still not getting a popup... I can see that a postback is occuring, but no popup. Hmmm.. I'm going to have to study this one hard- critical part of this project is the popups of the
detailview.
Eugene1968
Member
90 Points
360 Posts
passing selected value from gridview to another form's textbox
Dec 28, 2012 03:30 PM|LINK
Is there a way to use a query string or some javascript to collect a value from a gridview's button-selected row's specific column, open another form as a popup, and have one of that form's textboxes be populated with that value?
vingo_mail
Member
38 Points
67 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 03:50 PM|LINK
www.vingomail.blogspot.com
Please 'Mark as Answer' if this post helped you.
sudheerkumar
Member
4 Points
7 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 04:04 PM|LINK
you can do one thing in gridview row databound event write the java script method i.e window.open(),below i mentioned like this.
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onclick", "window.open('Default.aspx?Code=" + e.Row.Cells[1].Text + "','Report','width=900,height=500,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no'); return false;");
}
I hope above one reach your requirement.
Eugene1968
Member
90 Points
360 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 04:04 PM|LINK
This makes sense... the receiving page gets:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.txtTransactionNumber.Text = Request.QueryString("Number")
End Sub
But I'm not sure how to get the Number from my gridview.... thanks for any additional help!
Private Sub btnSubmit_Click(sender As Object, e As System.EventArgs)
Response.Redirect("RenewalsDetails.aspx?Number=" + Me.Gridview1. .......... )
End Sub
sudheerkumar
Member
4 Points
7 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 04:27 PM|LINK
You pass the values from one page to another page using querstring right,here through query string the value is passed to targer page in target page load event you can retieve the value and its passed to u r textbox.
Eugene1968
Member
90 Points
360 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 05:12 PM|LINK
I'm not following how the Java and my gridview's button are responding.
I've written some not unsimilar code in another pair of web pages:
<script language="javascript">
function GetRowValue(val1) {
window.opener.document.getElementById("txtTransactionNumber").value = val1;
window.close();
}
</script>
<script runat="server">
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Gridview1.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
DirectCast(e.Row.FindControl("btnSelect"), Button).Attributes.Add _
("onclick", "javascript:GetRowValue('" & e.Row.Cells(1).Text & "')")
End If
End Sub
Private Sub btnSubmit_Click(sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "window.open('RenewalsDetails.aspx?Number=" + e.Row.Cells(1).Text + "','Report','width=900,height=500,toolbar=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no'); return false;")
End If
End Sub
</script>
diverguy
Member
262 Points
427 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 06:59 PM|LINK
Several responses voted for passing the query string value to the popup page and I agree. Also, sudheerkumar is on the right track to inject javascript into the page. However, it seems like you might be overthinking this a little.
You have a gridview with some data and each row has a button. When the button is clicked, you want a separate window to pop up with details in it that are based on the gridview row that was clicked. Right, so far? Here's the easy way to do that:
If you haven't already done so, use the smart tag wizard to convert the gridview's command field to a template field. You will then be able to access the mark up for the button. Notice that the button has an attribute called CommandName. This is the action that will be performed when the button is clicked. Yours might say CommandName="Select" or something similar. There are several pre-baked command names like Select, Edit, Update, Cancel, Insert, etc. However, you can change those so that the command name does what you want it to do. Start off by changing your CommandName to something like ViewDetails. CommandName="ViewDetails"
Next, your code-behind needs the ID value or whatever your popup page will use in the query string. Another attribute of the button is CommandArgument. Use a server tag to pass the data key that you want in your query string. For example:
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="ViewDetails" CommandArgument='<%# Eval("ID") %>' Text="Edit"> </asp:LinkButton>This button has the CommandName of ViewDetails and it takes in the CommandArgument of the ID for that row. (The ID does not have to actually be displayed to your users)
Next, go to your code-behind and use the dropdown lists to create the gridview's RowCommand event. Inside the RowCommand event, check the CommandName being passed in and inject the javascript to open the popup accordingly. This code should get you pretty close:
If e.CommandName = "ViewDetails" Then Dim str As String = ("<script language='javascript'>" & "window.open('YourPageName.aspx?cn=") + e.CommandArgument & "', 'PopUp'," & "'top=0, left=0, width=500, height=500, menubar=0,toolbar=0,status=0,resizable=yes')" If Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "PopUp") Then ClientScript.RegisterClientScriptBlock(Me.[GetType](), "PopUp", str) End If End IfNotice that the ID from the html mark up is being passed in through CommandArgument and the code above is using that value in e.CommandArgument.
That's it. Click the button for any row in the gridview and the corresponding record should pop up in a separate window.
Now... if your page has an UpdatePanel (which I hate) you'll need to change the last two lines about the Client Script to this:
UpdatePanel1 would be the name of your UpdatePanel.
Eugene1968
Member
90 Points
360 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 08:13 PM|LINK
We're close, it's making more sense, but I'm missing something, but let's not go there :)
Anyway, here is my markup and code behind as it stands (I included my datasource):
<script runat="server">
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.txtTransactionNumber.Text = Request.QueryString("Renewal_ID_Rec")
End Sub
</script>
<asp:SqlDataSource
ID="SqlDataSource1"
runat="server"
connectionstring="<%$ ConnectionStrings:TrackITConnectionString %>"
selectcommand="SELECT * FROM [tblRenewals] WHERE ([Renewal_ID_Rec] = @Renewal_ID_Rec)">
<SelectParameters>
<asp:ControlParameter ControlID="txtTransactionNumber" Name="Renewal_ID_Rec"
PropertyName="Text" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
___________________________________________________________
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)
If e.CommandName = "ViewDetails" Then
Dim str As String = ("<script language='javascript'>" & "window.open('RenewalsDetails.aspx?=") + e.CommandArgument & "', 'PopUp'," & "'top=0, left=100, width=800, height=1000, menubar=0,toolbar=0,status=0,resizable=no')"
If Not ClientScript.IsClientScriptBlockRegistered(Me.[GetType](), "PopUp") Then
ClientScript.RegisterClientScriptBlock(Me.[GetType](), "PopUp", str)
End If
End If
End Sub
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSelect"
runat="server"
Text="Detail"
height="23px"
CausesValidation="False"
CommandName="ViewDetails"
CommandArgument='<%# Eval("Renewal_ID_Rec") %>'/>
</ItemTemplate>
</asp:TemplateField>
diverguy
Member
262 Points
427 Posts
Re: passing selected value from gridview to another form's textbox
Dec 28, 2012 09:19 PM|LINK
We're close, Eugene. The preformatted string to inject as javascript is probably where the problem lies and that may be partly my fault as it has been a few years since I worked with VB so the formatting wasn't right on the first pass. Right now, you have:
Dim str As String = ("<script language='javascript'>" & "window.open('RenewalsDetails.aspx?=") + e.CommandArgument & "', 'PopUp'," & "'top=0, left=100, width=800, height=1000, menubar=0,toolbar=0,status=0,resizable=no')"
The compiler won't be able to make sense of that since the opening parenthesis is outside of the quote mark (and unneeded) and the closing one is inside the quote mark (and also unneeded). Also, the query string name is missing from the url. Let's clean it up a little. Try this, instead:
Dim str As String = "<script language='javascript'>window.open('RenewalsDetails.aspx?Renewal_ID_Rec=" & e.CommandArgument & "', 'PopUp', 'top=0, left=100, width=800, height=1000, menubar=0,toolbar=0,status=0,resizable=no'"
That should be closer.
Eugene1968
Member
90 Points
360 Posts
Re: passing selected value from gridview to another form's textbox
Dec 29, 2012 02:11 PM|LINK
The VB compiler had no issue with the changes and they make sense, but I'm still not getting a popup... I can see that a postback is occuring, but no popup. Hmmm.. I'm going to have to study this one hard- critical part of this project is the popups of the detailview.