ASP .Net Passing value from gridview using Session State (VB) - asp.net

I am classic asp programmer that is learning .net. Here is my problem:
I have a gridview with the following columns: Pkey, Name, Address. Lets say that I want to have a hyperlink on the Pkey field to pass that value to another page. I have been able to get this to work using querystrings. I would like to use session state to do this. I really don’t have any idea how to do this since the pkey is in the gridview. Any help would be appreciated.

Try this code :
First Page
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var firstCell = e.Row.Cells[0];
firstCell.Controls.Clear();
firstCell.Controls.Add(new HyperLink { NavigateUrl = "secondpage?pkey = " + firstCell.Text, Text = firstCell.Text, Target = "_blank" });
Session["PKEY"] = firstCell.Text;
}
}
Second Page :
string pkey = Convert.ToString(Session["PKEY"]);
label1.Text = pkey;

Related

Cannot find id of control inside gridview which inside another gridview

Can anyone help me on this?
I have a button(ImageButton1) inside a GridView (gvSdt), and gridview b is inside another Gridview (gvClass).
but it come out an error said "The name 'gvSdt' does not exist in the current context"
The code i used as below:
protected void gvSdt_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewRow row = gvSdt.SelectedRow;
int rowIndex = gvSdt.SelectedIndex;
string strValue = gvSdt.DataKeys[rowIndex].Value.ToString();
}
Since gvSdt is contained within another GridView, you won't be able to access it directly, as it is repeated content. Since gvSdt can exist multiple times, gvSdt would therefore refer to multiple GridViews.
This is made easy however, since you are using the SelectedIndexChanged event of the GridView in question. Notice the sender argument of the method. The object that gets passed there is actually the GridView. So just cast it as such.
protected void gvSdt_SelectedIndexChanged(object sender, EventArgs e)
{
GridView gvSdt = (GridView)sender;
GridViewRow row = gvSdt.SelectedRow;
int rowIndex = gvSdt.SelectedIndex;
string strValue = gvSdt.DataKeys[rowIndex].Value.ToString();
}

Why would formview disappear in Edit Mode

I know I must be missing something simple but I cannot find it so I will pose the question here. I have a formview with two templates (item and edititem).
The form is bound to the itemtemplate in the page_Load event and works fine. However, if is use !IsPostBack in the code-behind, the formview disappears when the edit button is clicked. If I remove the postback check from page_load, then the form view appears after the edit button is clicked.
The page does have viewstate enabled.
In general, what steps are needed to get the formview to transition between modes correctly?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
source = Session["Source"].ToString();
acctType = Session["AccountType"].ToString();
acctNumber = Convert.ToInt32(Session["AccountNumber"]);
if (source == "LifeLineDS")
{
ObjectDataSource fvObjDS = new ObjectDataSource();
fvObjDS.TypeName = "LifelineDataAccess.LifelineSubscriber";
fvObjDS.SelectMethod = "GetLifelineDSSubscriber";
fvObjDS.SelectParameters.Add(new Parameter("AcctType", TypeCode.String, acctType));
fvObjDS.SelectParameters.Add(new Parameter("AcctNumber", TypeCode.String, Session["AccountNumber"].ToString()));
fvObjDS.DataBind();
if (fvObjDS != null)
{
fvSubscriber.DataSource = fvObjDS; //subscriber.ToString();
fvSubscriber.DataBind();
initialProgramValue = (fvSubscriber.FindControl("txtEligibility") as TextBox).Text;
}
}
// more code for other sources...
}
protected void btnEdit_Click(object sender, EventArgs e)
{
fvSubscriber.ChangeMode(FormViewMode.Edit);
fvSubscriber.DataSource = Session["subscriber"]; //Adding this line resolved !IsPostBack problem
fvSubscriber.DataBind();
ObjectDataSource programsObjDS = new ObjectDataSource();
programsObjDS.TypeName = "LifelineDataAccess.LifelineSubscriber";
programsObjDS.SelectMethod = "GetPrograms";
DropDownList ddlEligibility = ((DropDownList)(fvSubscriber.FindControl("ddlEligibility")));
if (ddlEligibility != null)
{
ddlEligibility.DataSource = programsObjDS;
ddlEligibility.DataTextField = "ProgramName";
ddlEligibility.DataValueField = "ProgramName";
ddlEligibility.SelectedValue = initialProgramValue; // Set selected value to subscribers current program
ddlEligibility.DataBind();
}
}
This
fvSubscriber.ChangeMode(FormViewMode.Edit);
fvSubscriber.DataBind();
seems to not to set the data source. The rule is that either you have the DataSourceID set in the declarative part of your code (*.aspx, *.ascx) and the binding is done automatically upon each postback OR you bind programmatically which involves setting the data source and calling the DataBind().
My recommendation would be to move your ObjectDataSource to the declarative part of the code and set the DataSourceID on the FormView to the ID of the ObjectDataSource. This is clean and easy and the binding works always.

Object reference not set to an instance of an object. This happens while adding checkboxlist control dynamically

Object reference not set to an instance of an object.
protected void cmdSave_Click(object sender, EventArgs e)
{
string strNames = string.Empty;
CheckBoxList Chkboxx = (CheckBoxList)PlaceHolder1.FindControl("Chkbox");
foreach (ListItem em in Chkboxx.Items) //-------- (Showing error)
{
if (em.Selected)
{
strNames += em.Value + ", ";
}
}
string final_name = strNames.Substring(0, strNames.Length - 2);
lblNames.Text = final_name;
}
Actually I am adding Checkbox control dynamically :
protected void ddl_varient_SelectedIndexChanged1(object sender, EventArgs e)
{
string query = "select prd_vrtyvalue_id,varient_value from tbl_ProductVariety_Value where varient='" + ddl_varient.SelectedItem.Text + "' " +
" order by varient_value asc ";
DataTable abc = new DataTable();
SqlDataAdapter ada = new SqlDataAdapter(query, new CommonClass().connection());
ada.Fill(abc);
ChkboxList.ID = "Chkbox";
for (int i = 0; i < abc.Rows.Count; i++)
{
ChkboxList.Items.Add(new ListItem(abc.Rows[i]["varient_value"].ToString(), abc.Rows[i]["prd_vrtyvalue_id"].ToString()));
}
ChkboxList.RepeatColumns = 2;
PlaceHolder1.Controls.Add(ChkboxList);
}
Can Anybody tell me, what exactly i am doing wrong !
The way ASP.NET WebForms work is that the entire page is re-built during each post back. So, I imagine this is what is occuring:
Page gets "built" and includes only controls defined within your ASCX/ASPX file.
User clicks on DDL_VARIENT checkbox and the ChkboxList is added to PlaceHolder1
Form is rendered back to the user so they can see ChkboxList
Save button is clicked, causing another postback.
Page is re-built, setting all the controls back to what is defined within your ASPX/ASCX code. This does not include ChkboxList.
Your code is hit, ChkboxList no longer exists and you get your problem.
To fix, you could re-add your ChkboxList on Page_Load depending on the value of your DDL_VARIENT checkbox. If I were you though, I'd be tempted to define the ChkboxList within your ASPX/ASCX code and then set the visibility of the list depending on the value of the DDL_VARIENT checkbox within Page_Load.
I should add, the entire of the above is dependant upon you using ASP.NET WebForms. If you're using MVC then it's probably wrong.

How can I put the return of a method into the GridView in ASP.NET?

I have a GridView that gets it's data from the SQL database.
I would like to alter some of it using an external method, something like this:
SQL in:
ID:0
Then, the altered method will be called with 0 as a parameter, and will return some string that will be shows in the GridView.
Thank you, Mark.
Attach a handler to the RowDataBound event. You have full control to all of the cells in the GridViewRow, and can modify the text anyway you want. Example from MSDN:
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
// Display the company name in italics.
e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>";
}
}
Reference at: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

How do I send a value from a gridview control button field to another page?

protected void GridView3_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Drill")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = GridView3.Rows[index];
TableCell sitenamecell = selectedRow.Cells[1];
string Site = sitenamecell.Text;
Session["Location"] = Site;
lblSession.Text = Session["Location"].ToString();
Response.Redirect("DisplayForm.aspx");
I am assuming your other page is the page you are redirecting to?
If so just add whatever you want to the query string:
Response.Redirect("DisplayForm.aspx?yourParamNameHere=" + yourParamNameValue);
You can use the session but then you have to worry about the back button, forward buttons and multiple tabs (instances) messing up what is stored in the session. The safest method is to just make it part of the query string that your receiving page looks for.
You could do it as a GET variable:
GridViewRow selectedRow = GridView3.Rows[index];
TableCell sitenamecell = selectedRow.Cells[1];
string Site = sitenamecell.Text;
Session["Location"] = Site;
lblSession.Text = Session["Location"].ToString();
Response.Redirect("DisplayForm.aspx?site=" + Site);

Resources