GridView select - passing values to textboxes on another form - c#

I have a GridView with the select button turned on.
<asp:CommandField ShowSelectButton="True" />
I want to pass the row values to TextBoxes that are on another page. So I am just working with on to test with the moment.
I am currently using the following code.
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
CreateSurvey CS = new CreateSurvey();
CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
This is my code from the second page (CreateSurvey.aspx)
public void PropDetails()
{
var Property_Name = Session["Property_Name"];
Create_PropName.Text = Property_Name.ToString();
}
The "CreateSurvey.aspx" page opens but the Create_PropName TextBox is empty.
Am I missing something?

try this
if you wan't to use session
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string Property_Name;
Property_Name = GridView1.SelectedRow.Cells[4].Text;
Session["Property_Name"] = Property_Name;
//you don't need this as you already set session above
//CreateSurvey CS = new CreateSurvey();
//CS.PropDetails();
Response.Redirect("CreateSurvey.aspx");
}
while receiving you just need to call below code in page load
if(Session["Property_Name"] != null)
Create_PropName.Text = Session["Property_Name"].ToString();
if you want to use query string
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("CreateSurvey.aspx?PropName="+GridView1.SelectedRow.Cells[4].Text);
}
in second page load
if(Request.QueryString["Property_Name"] != null)
Create_PropName.Text = Request.QueryString["Property_Name"];

Related

Data not loading on pageload with postback

I am having a problem. Data is not loading once the page loads. I have to choose an item on the dropdown list for it to load. I need it to load even before I choose any item on the drop down list.
This is my code behind.
protected void ddlPeriodStamp_SelectedIndexChanged(object sender, System.EventArgs e)
{
string selectedGroup = string.Empty;
DropDownList ddlItemGroup = (DropDownList)sender;
if (ddlItemGroup.SelectedValue != null)
TreatmentGroup = ddlItemGroup.SelectedValue;
ApplyGridFilter(ddlItemGroup.SelectedValue);
}
protected void ApplyGridFilter(string TreatmentGroup)
{
string selectedGroup = string.Empty;
DBDataSource1.State.BusinessObject.DataPump.FormFilters.Clear();
DBDataSource1.State.BusinessObject.DataPump.FormFilters.Add("TreatmentGroup", TreatmentGroup);
DBDataSource1.State.BusinessObject.Fill(null);
MedicalSchemeDetailGrid.DataBind();
}
protected void Page_LoadComplete(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
ApplyGridFilter(string.Empty);
}
Call ApplyGridFilter(string.Empty); while ispostBack is false and call ApplyGridFilter(ddlItemGroup.SelectedValue); while ispostBack is true
protected void Page_LoadComplete(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
ApplyGridFilter(ddlItemGroup.SelectedValue);// it will hit on first time page load
}
else
{
ApplyGridFilter(string.Empty);// it will hit while you change the dropdown items, But you should set true for **IsAutoPostBack** property on dropsownlist.
}
}
You need to fill your data in Page_Load event not in Page_LoadComplete event. According to MSDN:
The LoadComplete event occurs after all postback data and view-state
data is loaded into the page and after the OnLoad method has been
called for all controls on the page.
protected void Page_Load(object sender, System.EventArgs e)
{
if (!this.IsPostBack)
ApplyGridFilter(string.Empty);
}

using C# solution for Listview label

I want to use this solution to convert URLs to link in a listview label.
private string ConvertUrlsToLinks(string text)
{
string regex = #"((www\.|(http|https|ftp|news|file)+\:\/\/)[_.a-z0-9-]+\.[a-z0-9\/_:#=.+?,##%&~_-]*[^.|\'|\# |!|\(|?|,| |>|<|;|\)])";
System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(regex, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
return r.Replace(text, "$1").Replace("href=\"www", "href=\"http://www");
}
I have tried this in the listview databound but its not working.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
Label ProjectPostLabel = (Label)e.Item.FindControl("ProjectPostLabel");
ProjectPostLabel = ConvertUrlsToLinks({0});
}
Thank you
According to your code you are using ASP.NET Web Forms, not MVC.
You must use your ProjectPostLabel instance with Text property - no need to create a new label that is not assign to anywhere.
From your event you must retrieve Url property, not the label control. I have used NorthwindEmployee class with URL property in my ListView. You must cast it to your own class that is used in the list view.
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text = ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL);
}
And you must remember that only the last item from your list view will be displayed in the label (unless you expect that behavior). If you want to list of URLs from the list you can write this:
protected void Page_Load(object sender, EventArgs e)
{
ProjectPostLabel.Text = string.Empty;
}
protected void ProjectRecentActiviyListView_ItemDataBound(object sender, ListViewItemEventArgs e)
{
ProjectPostLabel.Text += string.Format("{0}<br/>", ConvertUrlsToLinks(((NorthwindEmployee)e.Item.DataItem).URL));
}

Pagination of GridView in ASP.NET (Won't go to page 2,3, etc.)

I have a Gridview and I want to set Pagination. So far I have:
<asp:GridView ID="grdActivity"
runat="server"
AutoGenerateColumns="False"
AllowPaging="True"
PageSize="30"
OnPageIndexChanging="gridView_PageIndexChanging">
C# code on the back is:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataBind();
}
I get no errors but when I press any other page (2 and on), nothing shows, just a blank page and the GridView disappears. Am I missing something? I see hundreds of pages showing this exact code to do this.
You will need to re-assign your datasource property. After the postback the grActivity.DataSource is null, and calling DataBind on it will clear the grid.
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
//re-assign your DataSource
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
//Set the new page index
grdActivity.PageIndex = e.NewPageIndex;
//apply your changes
grdActivity.DataBind();
}
Some notes:
you should consider re-factoring the code that brings you the data into a single method called on Page_Load and inside the gridview_PageIndexChanging (see #ekad's answer)
if you have a lot of data you should consider paging the data directly on the database. Right now if your table has 500 rows all will be loaded into the dataset even if just a small part of it (the page size) will be displayed in the grid
Hi you please keep the code in a separate method and call it in page load event and page index changed event, something like
protected void FillGrid()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
grdActivity.DataSource = myDataSet.Tables["tblActivity"];
grActivity.DataBind();
}
and in the page index changing event
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
FillGrid();
}
and in page load in !post back event simply call the method, like
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillGrid();
}
}
You have to set the data source of grdActivity before calling grdActivity.DataBind() in gridView_PageIndexChanging method. I would also suggest creating a separate method to avoid repeating the same code in Page_Load and gridView_PageIndexChanging
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}
}
private DataTable GetDataSource()
{
dsActivity myDataSet = new dsActivity();
myDataSet = clsDataLayer.GetActivity(Server.MapPath("DB.mdb"));
return myDataSet.Tables["tblActivity"];
}
protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdActivity.PageIndex = e.NewPageIndex;
grdActivity.DataSource = GetDataSource();
grdActivity.DataBind();
}

get value from ascx user control to aspx page

I have two files in my project. One is user control (popup) customerpicker.ascx and one is default.aspx page. In customerpicker I have dynamically generated gridview and 'select' column with SelectButton.
What I want is this: When I click on 'select' on random row in gridview, then I like to display value from selected row immediately (like ajax) to aspx.page. How it is possible?
There is part of my code in .ascx:
public string showOnaspx { get; set; }
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
showOnaspx = row.Cells[1].Text;
e.Cancel = true;
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = GridView1.SelectedRow;
}
I tried casting, get/set methods, but nothing worked for me.
Answer on that question is pretty easy.. We can save variabile in session and then read it in aspx.page wherever we want.
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];
Session["sessionid"]= row.Cells[1].Text;
e.Cancel = true;
}
Feeling so stupid right now, because I spent few days to figure it out simple way to do that.

Modify page_load method for radiobuttonlist

I have a page with radio buttons and a textarea that populates data dynamically based on your selection. The radio buttons act as a list of article titles and on selection you see the content of the article.
Within my pageload method, I want to allow users to be able to see a URL in their browser that points to value they've. That way they can link to the article within another source.
Currently, the method I have allows me to link to the button selection if I manually type in the following example URLs:
http://localhost/test/Articles_test.aspx?selected=1
http://localhost/test/Articles_test.aspx?selected=2
I'd like to modify this so that the URL appears in the browser when a radio button selection is made. Plus, on page load defaults to the "0" index if no value parameter was specified.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
int selected;
if (int.TryParse(Request.QueryString["selected"], out selected))
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "frm_Articles.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}
Set your radiobutton list to post back on change. Then, in the handler, do a redirect to the appropriate URL:
protected void Page_Load(object sender, EventArgs e)
{
int selected;
if (int.TryParse(Request.QueryString["selected"], out selected))
RadioButtonList1.SelectedIndex = selected;
RadioButtonList1.DataBind();
}
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
string strRedirect;
strRedirect = "frm_Articles.aspx?selected=" + RadioButtonList1.SelectedIndex;
Response.Redirect(strRedirect);
}

Categories