Hello all, need help please. I am having a page where I received data in my textbox from database using repeater, now I want to send this data of the textbox to another page on button click. Help please.
here is my code :
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Dharam RaiPosted Mar 9, 2016, 4:07 AM
Rafnas T PPosted Mar 5, 2016, 6:29 AM
Rafnas T PPosted Mar 2, 2016, 5:40 AM
{
TextBox _txtname = (TextBox ) item.FindControl("txtName");
if (_txtname != null)
{
Response.Redirect("Default2.aspx?_name="+_txtname.Text);
}
}
//in Page Load
protected void Page_Load(objec
{
if(!IsPostBack)
{
string _Textbox_val = Request.QueryString["_name"];
}
}
ali tuncerPosted Feb 16, 2016, 3:13 AM
Code Francis provided is for demo purpose. It does not connect to database.
It gets data from DataTable which is created in GetTable method.
It binds to repater in this line, DataSourceID in aspx page is not needed.
rptSample.DataSource = dt;
rptSample.DataBind();
I guess if you write this, it will work:
rptSample.DataSource = SqlDataSource1;
rptSample.DataBind();
You can also check this link for connecting to database and filling data table from code behind, connection string is defined in cs page, not aspx..
http://stackoverflow.com/questions/6073382/read-sql-table-into-c-sharp-datatable
Francis SusaimichaelPosted Feb 10, 2016, 5:23 AM
Dharam RaiPosted Feb 10, 2016, 4:44 AM
Francis SusaimichaelPosted Feb 10, 2016, 1:57 AM
I have formatted your code, Just copy it and paste it in "Gallery.aspx" and its codebehind file.
Gallery.aspx:
// });
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
using System.Data.Common;
namespace EventGallery.Gallery
{
public partial class Gallery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = GetTable();
rptSample.DataSource = dt;
rptSample.DataBind();
}
}
// contsturct temp data table for example purpose
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("ImageName", typeof(int));
// Here we add five DataRows.
table.Rows.Add(25);
table.Rows.Add(50);
table.Rows.Add(10);
table.Rows.Add(21);
table.Rows.Add(100);
return table;
}
protected void rptSample_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "SendData")
{
// Get the Text box instance
TextBox txt = (TextBox)e.Item.FindControl("txtName");
// Get the data from it
string strTextBoxValue = txt.Text;
//store it in session
Session["TextData"] = strTextBoxValue;
// redirect to another page
Response.Redirect("WebForm1.aspx");
}
}
}
}
Posted Feb 9, 2016, 7:42 AM
Dharam RaiPosted Feb 9, 2016, 7:22 AM
Dharam RaiPosted Feb 9, 2016, 7:10 AM
Posted Feb 9, 2016, 6:56 AM
Francis SusaimichaelPosted Feb 9, 2016, 6:50 AM
The reason for you error is, the method "rptSample_ItemCommand" NOT defined in your codebehind file. Take a close look on the sample that I provided once again, in the previous post. "Demo.aspx.cs" contains the method named "rptSample_ItemCommand". In that particular method alone I take the text box value and put it in session and redirect to another page.
Also I would suggest, create 2 pages (Demo.aspx and Webform1.aspx) in a separate project and copy my code and try to debug it for better understanding.
Dharam RaiPosted Feb 9, 2016, 6:44 AM
Shweta LodhaPosted Feb 9, 2016, 6:20 AM
you can store data as Session["AnyVariableName"] = YourDataToBeSent;
you can retrieve data as YourVariableToSaveData = Session["AnyVariableName"];
Dharam RaiPosted Feb 9, 2016, 6:05 AM
Francis SusaimichaelPosted Feb 9, 2016, 2:42 AM
I have wrote the complete code for your with repeater. Try this code and I'm sure it will solve your problem. Let me know if any issues.
Demo.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Demo.aspx.cs" Inherits="GridviewDemo.Demo" %>
Demo.aspx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace GridviewDemo
{
public partial class Demo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = GetTable();
rptSample.DataSource = dt;
rptSample.DataBind();
}
}
// contsturct temp data table for example purpose
static DataTable GetTable()
{
// Here we create a DataTable with four columns.
DataTable table = new DataTable();
table.Columns.Add("Age", typeof(int));
// Here we add five DataRows.
table.Rows.Add(25);
table.Rows.Add(50);
table.Rows.Add(10);
table.Rows.Add(21);
table.Rows.Add(100);
return table;
}
// This event called when you click the button
protected void rptSample_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "SendData")
{
// Get the Text box instance
TextBox txt =(TextBox) e.Item.FindControl("txtName");
// Get the data from it
string strTextBoxValue = txt.Text;
//store it in session
Session["TextData"] = strTextBoxValue;
// redirect to another page
Response.Redirect("WebForm1.aspx");
}
}
}
}
Webform1.aspx.cs
using System;
namespace GridviewDemo
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get the session value
Response.Write("Received Data is :" + Session["TextData"]);
}
}
}
Dharam RaiPosted Feb 9, 2016, 1:33 AM
Dharam RaiPosted Feb 9, 2016, 1:30 AM
Dharam RaiPosted Feb 9, 2016, 1:28 AM
Dharam RaiPosted Feb 9, 2016, 1:20 AM
Munesh SharmaPosted Feb 9, 2016, 12:04 AM
Rajeesh MenothPosted Feb 8, 2016, 11:04 AM
Francis SusaimichaelPosted Feb 8, 2016, 10:22 AM
My previous post is useless, since you are using "Repeater". So I would suggest, follow the below url, it will assist you for the solution:
http://stackoverflow.com/questions/2484806/accessing-textboxes-in-repeater-control
Dharam RaiPosted Feb 8, 2016, 7:34 AM
Francis SusaimichaelPosted Feb 8, 2016, 7:03 AM
You can use Session object for this purpose.
Webform1.aspx
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void SendData_Click(object sender, EventArgs e)
{
// Get the text box value
string strName = txtName.Text;
// Store it in session object
Session["SendData"] = strName;
//Redirect to another page
Response.Redirect("WebForm2.aspx");
}
}
}
webform2.aspx.cs
{
public partial class WebForm2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//Get the session value
Response.Write("Received Data is :" + Session["SendData"]);
}
}
}