Click here to Skip to main content
15,906,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Grid View Control, whenever the page loads all the available Data will display on my GridView and i place a Link Button on each row, now my problem is Whenever i Click on pay button the page will Redirect to another page by taking the details of that particular Row only. Below is my code....
ASP.NET
<asp:GridView ID="gvap" runat="server" AutoGenerateColumns="false" >
  <Columns>
  <asp:BoundField HeaderText="Course" DataField="course_name" />
  <asp:BoundField HeaderText="Start Date" DataField="start_date" />
  <asp:BoundField HeaderText="End Date" DataField="end_date" />
  <asp:BoundField HeaderText="Timings" DataField="timings" />
  <asp:BoundField HeaderText="Amount" DataField="amount" />
  <asp:TemplateField>
  <ItemTemplate>
  <asp:LinkButton runat="server" ID="lbtn" Text="Pay" PostBackUrl="~/EBS.aspx"></asp:LinkButton>
  </ItemTemplate>
  </asp:TemplateField>
  </Columns>
  </asp:GridView>

C#
protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=(local); Initial Catalog=123; User Id=sa; Password=123; Integrated Security=false";


        //Assigning Query
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select course_name , start_date , end_date, timings, amount from courses where start_date>=GETDATE()";
        cmd.CommandType = CommandType.Text;
        

        //Execute the COmmand
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvap.DataSource = ds;
        gvap.DataBind();    
    }
Posted
Comments
[no name] 28-Oct-13 2:11am    
remove navigate url in ur pay button, use row command event of grid view and fetch data of row from naming container... pass a command along with link button and apply logic in row command event
Madhu Nair 28-Oct-13 2:57am    
post this as Answer!!!

What shabarinadh.vempati is trying to say is -

Add 'OnRowCommand' event to your gridview like-
C#
<asp:gridview id="gvap" runat="server" autogeneratecolumns="false" onrowcommand="gvap_RowCommand" xmlns:asp="#unknown"></asp:gridview>


and add commandname property to your linkbutton

and then on its eventhandler in code behind file do something like -
C#
protected void gvap_RowCommand(objectsender,GridViewCommandEventArgs e)
    {
        if (e.CommandName == "EditMe")
        {
            GridViewRow grow = (GridViewRow)(((linkbutton)e.CommandSource).NamingContainer);
            hdn.Value = grow.Cells[1].Text;


        }

do something like this try to access the grid's row values using any state management technique..

One more thing write the whole sql interation and databinding in a function and call it on page load with a (!ispostback)like -

C#
public void populategrid()
{
SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=(local); Initial Catalog=123; User Id=sa; Password=123; Integrated Security=false";
 

        //Assigning Query
        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select course_name , start_date , end_date, timings, amount from courses where start_date>=GETDATE()";
        cmd.CommandType = CommandType.Text;
        
 
        //Execute the COmmand
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds);
        gvap.DataSource = ds;
        gvap.DataBind(); 
}


and then on page load
C#
if(!ispostback)
{
populategrid();
}


hope this helps!
 
Share this answer
 
Comments
Venkat Kumar chigulla 28-Oct-13 5:16am    
Thanks a lot it's working, now i can access Grid View Rows.....

I have a doubt that why should i use Hidden Fields here rather than String

String s = grow.Cells[1].Text;

One Morething that i want to display the Selected Row data in Next page, I used Query String but the problem is that it shows the Data in URL..... i want to hide my data in URL is there any alternative
[no name] 28-Oct-13 7:50am    
then use sessions

:)
Venkat Kumar chigulla 29-Oct-13 1:19am    
By using sessions i can pass the values from one page to another page, Here i observed one thing that for example i pass the values from send.aspx page to receive.aspx page it's working. Now i remember the URL, this time I directly paste the URL(receive.aspx) it shows previous data but it means wrong is it...? can we do anything here
I added a code in LinkButton
ASP.NET
<asp:linkbutton runat="server" id="lbtn" text="Pay" commandargument="<%# Eval("amount") %>" commandname="Select" xmlns:asp="#unknown"></asp:linkbutton>

  <asp:label id="lblMessage" runat="server" xmlns:asp="#unknown"></asp:label>   
    
  <asp:textbox id="txtMobile" runat="server" xmlns:asp="#unknown"></asp:textbox>


Added In my code Behind

C#
protected void gv_rowcommand(object sender, GridViewCommandEventArgs e)
   {
       if (e.CommandName == "Select")
       {
           txtMobile.Text = Convert.ToString(e.CommandArgument);
           lblMessage.Text = "Amount = ";

       }
   }



It will display the Selected Row Amount.....

Now i want to Display Entire Row Information like that......
 
Share this answer
 
One way i have for this
at the time page load when first time data diplayed
onRowdatabound event of grid you can add attribute postback url to link button
by appending all column values in query string now when you will click on link it redirect you to that page on that you can fetch all values from this string.

Hope this helps!!!
 
Share this answer
 
Assign the gridview values to variables like this in rowcommand eventhandler-

C#
string co = grow.Cells[1].Text;
string date = grow.Cells[2].Text;


You can use sessions,query string etc -
C#
//and then concatenate these string variable and add with the targeted aspx page like-
Response.Redirect("Default2.aspx?course=" + co+ "&date=" + date);


simillarly if you want to add more values just add '&' and concatenate..

On default2.aspx

use textbox or label(whatever you want)to display the information like..
C#
coursetxt.text=Request.QueryString["course"]


hope this helps!
 
Share this answer
 
Comments
Venkat Kumar chigulla 28-Oct-13 7:37am    
If we use Querystring the information will show in the URL, is there any alternative for this..?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900