New to ASP.Net. Been looking for a similar solution but no luck. I have two fields on page1 - Button1 and TextBox1. Button1 calls ModalPopup that has a gridview contained within it. Everything works. Gridview col 1 is a linkbutton. I'd like to select
a row within col1 of gridview and return the value to TextBox1. Problem now is that I select a link button in gridview and the value is not posted back. Hope this makes sense. Thanks.
Nizam, thanks for the input. I reading up on delegates but not clear on how to use them. Do you know if it is possible to return the value from the grid using syntax similar to this: Page(MyASPPage.aspx).TextBox1.Text.Value = "SomeValue" ? I may be over-simplifying
ASP.
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
TextBox1.Text = row.Cells[7].Text;
}
ContactID was added as the last column and set to invisible after binding to the gridview. This apparently makes the value retrieveable. Not sure if this is very sophisticated but it works.
DonInOC
0 Points
5 Posts
Gridview in ModalPopUp does not postback selected value
Jun 29, 2012 07:31 AM|LINK
New to ASP.Net. Been looking for a similar solution but no luck. I have two fields on page1 - Button1 and TextBox1. Button1 calls ModalPopup that has a gridview contained within it. Everything works. Gridview col 1 is a linkbutton. I'd like to select a row within col1 of gridview and return the value to TextBox1. Problem now is that I select a link button in gridview and the value is not posted back. Hope this makes sense. Thanks.
karthicks
All-Star
31376 Points
5421 Posts
Re: Gridview in ModalPopUp does not postback selected value
Jun 29, 2012 08:53 AM|LINK
hi, here is a sample code
<asp:Panel ID="pnlAdd" runat="server" BackColor="LightGray" Style="display: none" CssClass="modalPopup"> <div id="divHeading" runat="server" style="border: 1px solid black;" onmouseover="this.style.cursor='move'"> <b>Test</b> </div> <asp:GridView ID="GridView1" runat="server" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> <Columns> <asp:ButtonField CommandName="Select" ButtonType="Button" Text="Select" /> </Columns> </asp:GridView> <asp:Button ID="btnOk" runat="server" Text="Ok" /> <asp:Button ID="btnCancel" runat="server" Text="Cancel" /> </asp:Panel> <cc1:ModalPopupExtender ID="pnlAdd_ModalPopupExtender" BehaviorID="pnlAdd" runat="server" DynamicServicePath="" Enabled="True" TargetControlID="lblAdd" PopupControlID="pnlAdd" CancelControlID="btnCancel" BackgroundCssClass="modalBackground" PopupDragHandleControlID="divHeading" Drag="false" RepositionMode="RepositionOnWindowResize" X="50" Y="50"> </cc1:ModalPopupExtender> <asp:Label ID="lblAdd" runat="server" Text=""></asp:Label> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Show Popup" /> <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { this.GridView1.DataSource = new String[] { "A", "B", "C" }; this.GridView1.DataBind(); } } protected void Button2_Click(object sender, EventArgs e) { this.pnlAdd_ModalPopupExtender.Show(); } protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { this.TextBox1.Text = this.GridView1.SelectedRow.Cells[1].Text; }Karthick S
nizam.mcts
Member
116 Points
38 Posts
Re: Gridview in ModalPopUp does not postback selected value
Jun 29, 2012 11:13 AM|LINK
Hi,
As karthicks said it may work for you if it is not working then create a delegate for that button which there in grid and try....
DonInOC
0 Points
5 Posts
Re: Gridview in ModalPopUp does not postback selected value
Jun 29, 2012 07:39 PM|LINK
Karthicks, thanks for looking at this. Unfortunately it is not working. I'm attaching my page (below).
<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Search.aspx.cs" Inherits="WebApplication7.Search" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:ModalPopupExtender ID="ModalPopupExtender1" runat="server" PopupControlID="Panel2"
DynamicServicePath="" Enabled="True" TargetControlID="dummyLink" BackgroundCssClass="modalBackground">
</asp:ModalPopupExtender>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
<!-- Hidden control attached to ModalPopupExtender -->
<a href="#" style="display: none; visibility: hidden;" onclick="return false" id="dummyLink"
runat="server">dummy</a>
<!-- Hidden control attached to ModalPopupExtender -->
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
<asp:Panel ID="Panel2" runat="server" class="modalPopup">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="ContactID" ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="false" CommandName="SelectContact"
Text='<%# Eval("ContactID") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="MiddleName" HeaderText="MiddleName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" />
<asp:BoundField DataField="Phone" HeaderText="Phone" />
</Columns>
</asp:GridView>
</asp:Panel>
<br />
<br />
</asp:Content>
protected void Button1_Click(object sender, EventArgs e)
{
string sql = "SELECT TOP 10 ContactID,Title,FirstName,MiddleName,LastName,EmailAddress,Phone FROM [AdventureWorks].[Person].[Contact]";
OdbcConnection myConnection = new OdbcConnection(ConfigurationManager.ConnectionStrings["DBAccess"].ConnectionString);
DataTable ProductDS = new DataTable();
OdbcDataAdapter adapter = new OdbcDataAdapter(sql, myConnection);
adapter.Fill(ProductDS);
myConnection.Open();
GridView1.DataSource = ProductDS;
GridView1.DataBind();
myConnection.Close();
ModalPopupExtender1.Show();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
this.TextBox1.Text = this.GridView1.SelectedRow.Cells[1].Text;
}
So, the idea here is to pick one of the 10 values in the Gridview and return the SelectedRow to TextBox1.
Thanks again.
DonInOC
0 Points
5 Posts
Re: Gridview in ModalPopUp does not postback selected value
Jun 29, 2012 08:56 PM|LINK
Nizam, thanks for the input. I reading up on delegates but not clear on how to use them. Do you know if it is possible to return the value from the grid using syntax similar to this: Page(MyASPPage.aspx).TextBox1.Text.Value = "SomeValue" ? I may be over-simplifying ASP.
Thanks
Don
DonInOC
0 Points
5 Posts
Re: Gridview in ModalPopUp does not postback selected value
Jun 30, 2012 12:27 AM|LINK
Getting closer ...
Changed _SelectIndexChanged to _OnRowCommand(object sender, GridViewCommandEventArgs e) so ...
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
TextBox1.Text = row.Cells[0].ToString();
}
Any idea on how to get the actual value or Col 1 (not the indexed value)?
Grid displays (where col 1 is a link button which I cannot display here; just imagine that it looks like a link)
Col1 Col2 Col3
10 Mr. Smith
11 Ms. Jone
12 Mrs. Robinson
row.Cells[0].ToString() returns 0, 1,or 2 to TextBox1 when 10, 11, or 12 is selected (respectively).
row.Cells[1].ToString() returns Mr., Ms., or Mrs. to TextBox1 depending on row selected.
I need TextBox1 to display 10, 11 or 12.
Thanks.
karthicks
All-Star
31376 Points
5421 Posts
Re: Gridview in ModalPopUp does not postback selected value
Jun 30, 2012 04:04 AM|LINK
hi, if you want to get value from LinkButton, you can refer below code
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e) { int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = GridView1.Rows[index]; TextBox1.Text = (row.FindControl("LinkButton1") as LinkButton).Text; //(Or) TextBox1.Text = (e.CommandSource as LinkButton).Text; }my earlier post code also will work but you did not give CommandName="Select".
Mark my both the post as answered one
Karthick S
DonInOC
0 Points
5 Posts
Re: Gridview in ModalPopUp does not postback selected value
Jun 30, 2012 05:10 AM|LINK
Hey Karthiks
I must be doing something wrong with your code. I ended up doing the following (for anyonelse looking to do the same). Gridview is now:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_OnRowCommand">
<Columns>
<asp:ButtonField CommandName="GetContactID" DataTextField="ContactID" HeaderText="ContactID" Text="ContactID" />
<asp:BoundField DataField="Title" HeaderText="Title" />
<asp:BoundField DataField="FirstName" HeaderText="FirstName" />
<asp:BoundField DataField="MiddleName" HeaderText="MiddleName" />
<asp:BoundField DataField="LastName" HeaderText="LastName" />
<asp:BoundField DataField="EmailAddress" HeaderText="EmailAddress" />
<asp:BoundField DataField="Phone" HeaderText="Phone" />
<asp:BoundField DataField="ContactID" HeaderText="ContactID" />
</Columns>
</asp:GridView>
protected void Button1_Click(object sender, EventArgs e)
{
string sql = "SELECT TOP 10 ContactID,Title,FirstName,MiddleName,LastName,EmailAddress,Phone,ContactID FROM [AdventureWorks].[Person].[Contact]";
OdbcConnection myConnection = new OdbcConnection(ConfigurationManager.ConnectionStrings["DBAccess"].ConnectionString);
DataTable ProductDS = new DataTable();
OdbcDataAdapter adapter = new OdbcDataAdapter(sql, myConnection);
adapter.Fill(ProductDS);
myConnection.Open();
GridView1.DataSource = ProductDS;
GridView1.DataBind();
GridView1.Columns[7].Visible = false;
myConnection.Close();
ModalPopupExtender1.Show();
}
protected void GridView1_OnRowCommand(object sender, GridViewCommandEventArgs e)
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
TextBox1.Text = row.Cells[7].Text;
}
ContactID was added as the last column and set to invisible after binding to the gridview. This apparently makes the value retrieveable. Not sure if this is very sophisticated but it works.
Thanks to all for your input.