I have 2 pages both are using Gridview and implemented in C# asp.net.
On page 1, in the Gridview (ie, Edit, Delete enable) when I click Edit so I have a Textbox which allow me to
edit the text in the textbox that populated from the database and next to it I have a button when click I need
to open a page 2 where user can select a row (ie, page 2 also use Gridview, Select enable and use
different datasource ), once user select a row I need to get the value row.cells[1] for example and put the value
into the textbox in page 1 and page 2 close itself. the purpose of page 2 serve as a look up table and for user
to select a row, then display that value in the textbox in page1.
I can get the value on page 2 when user select a row but I dont know how to implement such when user
select a row and get the value on page 2 and put the value into the textbox in page 1 and page 2 close itself.
any suggestion and guidance is greatly appreciated.
thanks in advance.
for (int i = 0; i < GrdiView1.Rows.Count; i++)
{
if (GrdiView1.SelectedIndex == i)
{
String name =GridView1.Rows[i].Cells[1].Text; //Gets the data value in the grid;
Session["Value"] = name;
Response.Redirect("Page1.aspx");
Thank you all very much for your reply
The issue that I am facing is that on page 1, I have a Gridview with 2 fields from db and Edit enable
so when I click Edit on a particular row let say it should shows 2 TextBox, 2 buttons ( Lookup1, Lookup 2 ), Update and Cancel button
so when I click on the button next to the TextBox 1, for example Lookup 1,
<
EditItemTemplate>
<asp:TextBox
ID="TextBox1"
runat="server"></asp:TextBox>
<asp:Button
ID="lookup1"
runat="server"
Text="Lookup"
CssClass="editCmdStyle"
/>
</EditItemTemplate>
a new page should open, and I suppose to pass along the
Id of the TextBox 1 next to the button so that on page 2 when user select a row, I just need to get the value and assign to the Textbox in page 1.
I just dont know how to get the TextBox Id to pass along to page 2, I tried the OnRowEditing="gridview_OnRowEditing" but I got a NullException, I also tried
OnRowDataBound="gridview_RowDataBound" it's also NullException
TextBox tb = (TextBox)e.Row.Cells[1].FindControl("TextBox1");
or
TextBox tb
= (TextBox)GridView1.FindControl("TextBox1");
If I create a TextBox that is not in the Gridview that it is OK , the problem is the TextBoxId
Is there any way to get the TextBoxId when the Edit button is click so that I can pass along to page2 ?
I tried and tried and almost give up so pls pls pls help.
Thanks for your reply,
it's still null. I think because the Textbox in the Edit row is formed only after Edit is click
I even tried to get the TextId on OnRowEditing but still TextId is null
Any other suggestion is greatly appreciated.
Regards
BrigideW
Member
187 Points
138 Posts
Retrieve select row.cell from Gridview and display in Textbox
Dec 15, 2007 01:00 PM|LINK
Hi all,
Pls help with this issue:
I have 2 pages both are using Gridview and implemented in C# asp.net.
On page 1, in the Gridview (ie, Edit, Delete enable) when I click Edit so I have a Textbox which allow me to
edit the text in the textbox that populated from the database and next to it I have a button when click I need
to open a page 2 where user can select a row (ie, page 2 also use Gridview, Select enable and use
different datasource ), once user select a row I need to get the value row.cells[1] for example and put the value
into the textbox in page 1 and page 2 close itself. the purpose of page 2 serve as a look up table and for user
to select a row, then display that value in the textbox in page1.
I can get the value on page 2 when user select a row but I dont know how to implement such when user
select a row and get the value on page 2 and put the value into the textbox in page 1 and page 2 close itself.
any suggestion and guidance is greatly appreciated.
thanks in advance.
Allen Chen –...
All-Star
40943 Points
4949 Posts
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 17, 2007 04:46 AM|LINK
Hi:
Here's the sample:
Page1:
<input id="TextBox1" type="text" />
<input onclick="window.open('Page2.aspx')" id="Button3" type="button" value="button" />
Page2:
<input onclick="window.opener.document.getElementById('TextBox1').value='Hello';self.close();" id="Button1" type="button" value="button" />
Regards
Allen Chen
Microsoft Online Community Support
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 17, 2007 05:00 AM|LINK
Since you are using the SELECT CommandField then you can have something like this
Page2.Aspx
protected void Button1_Click(object sender, EventArgs e)
{
for (int i = 0; i < GrdiView1.Rows.Count; i++)
{
if (GrdiView1.SelectedIndex == i)
{
String name =GridView1.Rows[i].Cells[1].Text; //Gets the data value in the grid;
Session["Value"] = name;
Response.Redirect("Page1.aspx");
}
}
}
Page1.aspx
protected void Page_Load(object sender, EventArgs e)
{
string RowValue = Session["Value"].ToString();
TextBox1.Text = RowValue;
}
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
hardikpatel
Participant
1052 Points
206 Posts
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 17, 2007 05:31 AM|LINK
You can create session for seleted value in grid in page2.aspx and get that session value on page1.aspx on click event of page2.aspx button
Visit my website Code Tech Blog for Latest Microsoft's updates and Coding Tips
Please remember to click "Mark as Answer" on this post if it helped you.
BrigideW
Member
187 Points
138 Posts
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 17, 2007 10:20 PM|LINK
Hi Allen,
Thank you all very much for your replyThe issue that I am facing is that on page 1, I have a Gridview with 2 fields from db and Edit enable
so when I click Edit on a particular row let say it should shows 2 TextBox, 2 buttons ( Lookup1, Lookup 2 ), Update and Cancel button
so when I click on the button next to the TextBox 1, for example Lookup 1,
<
EditItemTemplate><asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="lookup1" runat="server" Text="Lookup" CssClass="editCmdStyle" />
</EditItemTemplate>
a new page should open, and I suppose to pass along the
Id of the TextBox 1 next to the button so that on page 2 when user select a row, I just need to get the value and assign to the Textbox in page 1.
I just dont know how to get the TextBox Id to pass along to page 2, I tried the OnRowEditing="gridview_OnRowEditing" but I got a NullException, I also tried
OnRowDataBound="gridview_RowDataBound" it's also NullException
TextBox tb = (TextBox)e.Row.Cells[1].FindControl("TextBox1");
or
TextBox tb = (TextBox)GridView1.FindControl("TextBox1");
If I create a TextBox that is not in the Gridview that it is OK , the problem is the TextBoxId
Is there any way to get the TextBoxId when the Edit button is click so that I can pass along to page2 ?
I tried and tried and almost give up so pls pls pls help.
Truly appreciated
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 17, 2007 11:58 PM|LINK
Try this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach(GridViewRow row in GridView1.rows)
{
TextBox tb = (TextBox) row.Cells[1].FindControl("TextBox1");
//OR
TextBox tb = (TextBox) row.FindControl("TextBox1");
}
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
BrigideW
Member
187 Points
138 Posts
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 18, 2007 01:37 AM|LINK
Hi vinz,
Thanks for your replied
I tried and it's still null for
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach(GridViewRow row in GridView1.rows)
{
TextBox tb = (TextBox) row.Cells[1].FindControl("TextBox1") ;
TextBox tb1 = (TextBox) row.FindControl("TextBox1") ;
if ( tb = null || tb1 != null)
{
Response.write( TextBox tb = (TextBox) row.Cells[1].FindControl("TextBox1").Text );
}
}
and it's still null
vinz
All-Star
126896 Points
17922 Posts
MVP
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 18, 2007 02:21 AM|LINK
i think there is something wrong about your IF condition.. try this
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
foreach(GridViewRow row in GridView1.rows)
{
TextBox tb = (TextBox) row.Cells[1].FindControl("TextBox1") ;
if ( tb.Text != null || tb.Text != "")
{
Response.write( tb.Text );
}
}
NOTE: Besure that your textbox1 reside in Cells[1] or else you cannot get the value for TextBox1
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
BrigideW
Member
187 Points
138 Posts
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 18, 2007 12:13 PM|LINK
Hi Vinz,
Thanks for your reply,
it's still null. I think because the Textbox in the Edit row is formed only after Edit is click
I even tried to get the TextId on OnRowEditing but still TextId is null
Any other suggestion is greatly appreciated.
Regards
impathan
Contributor
4371 Points
836 Posts
Re: Retrieve select row.cell from Gridview and display in Textbox
Dec 18, 2007 12:42 PM|LINK
Why are you looping through gridview's row in rowdatabound event?
try this one
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
TextBox tb = (TextBox) e.row.FindControl("TextBox1") ; // debug this code
string val=tb.Text;
}
ImranKhan pathan