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.
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 8, 2016, 2:03 AM EST
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"]);
}
}
}
Dharam RaiRank #3,922 · 378 reputationPosted Feb 8, 2016, 2:34 AM EST
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 8, 2016, 5:22 AM EST
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
Rajeesh MenothRank #118 · 18.4K reputationPosted Feb 8, 2016, 6:04 AM EST
Munesh SharmaRank #174 · 13.9K reputationPosted Feb 8, 2016, 7:04 PM EST
Dharam RaiRank #3,922 · 378 reputationPosted Feb 8, 2016, 8:20 PM EST
Dharam RaiRank #3,922 · 378 reputationPosted Feb 8, 2016, 8:28 PM EST
Dharam RaiRank #3,922 · 378 reputationPosted Feb 8, 2016, 8:30 PM EST
Dharam RaiRank #3,922 · 378 reputationPosted Feb 8, 2016, 8:33 PM EST
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 8, 2016, 9:42 PM EST
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 RaiRank #3,922 · 378 reputationPosted Feb 9, 2016, 1:05 AM EST
Shweta LodhaRank #31 · 39.8K reputationPosted Feb 9, 2016, 1:20 AM EST
you can store data as Session["AnyVariableName"] = YourDataToBeSent;
you can retrieve data as YourVariableToSaveData = Session["AnyVariableName"];
Dharam RaiRank #3,922 · 378 reputationPosted Feb 9, 2016, 1:44 AM EST
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 9, 2016, 1:50 AM EST
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.
Posted Feb 9, 2016, 1:56 AM EST
Dharam RaiRank #3,922 · 378 reputationPosted Feb 9, 2016, 2:10 AM EST
Dharam RaiRank #3,922 · 378 reputationPosted Feb 9, 2016, 2:22 AM EST
Posted Feb 9, 2016, 2:42 AM EST
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 9, 2016, 8:57 PM EST
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");
}
}
}
}
Dharam RaiRank #3,922 · 378 reputationPosted Feb 9, 2016, 11:44 PM EST
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 10, 2016, 12:23 AM EST
ali tuncerRank #1,499 · 1.1K reputationPosted Feb 15, 2016, 10:13 PM EST
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
Rafnas T PRank #280 · 8.8K reputationPosted Mar 2, 2016, 12:40 AM EST
{
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"];
}
}
Rafnas T PRank #280 · 8.8K reputationPosted Mar 5, 2016, 1:29 AM EST
Dharam RaiRank #3,922 · 378 reputationPosted Mar 8, 2016, 11:07 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts