Passing DataKeys between pages - asp.net

Using a GridView on the default page and want to show details of the row selected on another page. The code for capturing and sending the datakey is -
protected void SelectedIndexChanged(object sender, EventArgs e)
{
int index = GridView1.SelectedIndex;
Response.Redirect("InvoicePage.aspx? EntityID= " + GridView1.DataKeys[index].Value.ToString());
}
And the code for retrieving the value is -
protected void Page_Load(object sender, EventArgs e)
{
string id = Request.QueryString["EntityID"];
}
My problem is that the id variable is a null on the receiving page. What am I missing?

Your URL should look something like this: InvoicePage.aspx?EntityID=", No space before or after EntityID

By The way Remove the white space : aspx?<>*EntityID<>*=
protected void SelectedIndexChanged(object sender, EventArgs e)
{
int index = GridView1.SelectedIndex;
string id = GridView1.DataKeys[index].Value.ToString();
Response.Redirect("InvoicePage.aspx?EntityID="+id);
}
Try to add :
private string _EntityId;
//To check if value pass on query string
//but this is not really required if you want
public string EntityId
{
get
{
if (Request.QueryString["EntityID"] != null)
{
try
{
_EntityId = Convert.ToString(Request.QueryString["EntityID"].ToString());
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
else
{
_EntityId="0";
}
return _EntityId;
}
}
protected void Page_Load(object sender, EventArgs e)
{
//Request.QueryString["EntityID"].ToString();
string id = EntityId;
}
Regads

Related

DateEdit value change

I would like to add minute (which is entered from a calcEdit) to starting date then it wil be set as end date.
Also when I enter the end date the subtract of start time will be set as minute .
I tried dateEdit's EditValueChanged ,Validating events and I tried both for calcedit but got wrong values.
I use g mask for dateEdits
Please help me thank you.
Here are my codes :
`private void calcEditMinute_Validating(object sender, CancelEventArgs e)
{
try
{
dtBitisZamani = Convert.ToDateTime(dateEditBas.EditValue).AddMinutes(Convert.ToDouble(calcEditMinute.Text));
dateEditBit.EditValue = dtBitisZamani;
}
catch (Exception)
{
}
}
private void dateEditBit_EditValueChanged(object sender, EventArgs e)
{
TimeSpan span = Convert.ToDateTime(dateEditBit.EditValue).Subtract(Convert.ToDateTime(dateEditBas.EditValue));
calcEditMinute.Text = string.Format(" {0} ",
span.Minutes); span.TotalMinutes.ToString();
}`
Try this:
private void calcEdit1_EditValueChanged(object sender, EventArgs e)
{
dateEditEnd.DateTime = dateEditStart.DateTime.AddMinutes(Convert.ToDouble(calcEdit1.Value));
}
private void dateEditEnd_EditValueChanged(object sender, EventArgs e)
{
dateEditStart.DateTime = dateEditEnd.DateTime.AddMinutes(Convert.ToDouble(calcEdit1.Value) * -1);
}

PageIndexChanging Event in search button in asp.net?

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
grid();
}
}
protected void btnsearch_Click(object sender, EventArgs e)
{
objRetailPL.ZoneName = ddlzone.SelectedValue;
//IFormatProvider provider = new System.Globalization.CultureInfo("en-CA", true);
//String datetime = txtdate.Text.ToString();
//DateTime dt = DateTime.Parse(datetime, provider, System.Globalization.DateTimeStyles.NoCurrentDateDefault);
//objRetailPL.date =dt;
DataTable dtsearch = new DataTable();
dtsearch = objRetailBAL.searchzone(objRetailPL);
GVRate.DataSource = dtsearch;
GVRate.DataBind();
}
protected void GVRate_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
try
{
GVRate.PageIndex = e.NewPageIndex;
grid();
}
catch (Exception ex)
{
}
}
I have this code for searching records.And for that grid i added page index,but it is not coming properly.When click on search button it is showing more n of records at that time page index is not working. It is calling First grid before clicking search button..How can I change my code please help me.....
Try this ...
private void grid()
{
if(!string.IsNullOrEmpty(yourSearchTextBox.Text)
{
// Then call search method. Make sure you bind the Grid in that method.
}
else
{
// Normally Bind the Grid as you are already doing in this method.
}
}
you have to check whether there is any search Text or not in that grid() method like...

How to make this asp .net code work?

Can anyone tell me what I'm doing wrong?
//--- menuFac ---
public void UpdatePageById()
{
db.ModifyData("UPDATE tblsider SET colOverskrift=#1, colTekst=#2 WHERE colID=#3", _overskrift, _tekst, _id);
}
//--- where i'm trying to get some from db to edit and save the edited ---
menuFac objTekst = new menuFac();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
objTekst._id = int.Parse(Request.QueryString["colID"]);
DataRow value = objTekst.GetPageById();
txtOverskrift.Text = value["colOverskrift"].ToString();
txtTekst.Text = value["colTekst"].ToString();
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
objTekst._id = int.Parse(Request.QueryString["colID"]);
objTekst._overskrift = txtOverskrift.Text;
objTekst._tekst = txtTekst.Text;
objTekst.UpdatePageById();
Response.Redirect("Protected.aspx");
}
Replace this call method
objTekst.UpdatePageById();
with
this.UpdatePageById();
UpdatePageById is method of your Page Class, not of your property objTekst

Have to press Enter Twice for a function to work C#

I have the following .aspx page. I'm searching for the words that are in the database, everything works fine. I also have a HyperLink which shows a message if the word does not exist in the database.
BUT the only problem is when I search for the word and the word is not in the database it doesn't show the HyperLink from the first time, I have to CLICK ENTER twice for it to work, and CLICK CLEAR twice for it to dissapear.
I know that the problem is that I have the button after the page_load, but I can't find the solution for it. What I've tried is created another function private void load_data() and copy everything from page_load into it. And in the load_page and button2_click just call load_data(); It doesn't work.
Here's the code:
namespace TRI_Portal.ScreenPop.Gloss
{
public partial class Gloss_Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (GridView2.Rows.Count == 0)
{
HyperLink1.Visible = true;
}
else
{
HyperLink1.Visible = false;
}
String s = Request.QueryString["language"];
Language1.Text = Server.HtmlEncode(s);
}
}
}
I have tried this as well, but no luck.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetLink();
}
}
private void SetLink()
{
if (GridView2.Rows.Count == 0)
{
HyperLink1.Visible = true;
}
else
{
HyperLink1.Visible = false;
}
String s = Request.QueryString["language"];
Language1.Text = Server.HtmlEncode(s);
}
protected void Button2_Click(object sender, EventArgs e)
{
SetLink();
}
Any suggestions?
Handle GridView.DataBound event
protected void GridView1_DataBound(object sender, EventArgs e)
{
SetLink();
}
And markup:
<asp:GridView OnDataBound="GridView1_DataBound" ....
Remove the code from the Page_Load event.

How to do that fields not initialization?

how to do that every time s_Sort not update SortDirection.Desc
private SortDirection s_Sort = SortDirection.Desc;
protected void Page_Load(object sender, EventArgs e)
{
lblSort.Text = S_Sort.ToString();//every time == SortDirection.Desc - this is bad!
if (!IsPostBack)
{
ShowTree();
Validate();
}
}
Need
public void btnSortUp_Click(object sender, EventArgs e)
{
S_Sort = SortDirection.Asc;
}
public void btnSortDown_Click(object sender, EventArgs e)
{
S_Sort = SortDirection.Desc;
}
but after SortDirection.Desc is bad
The is a problem of the ASP.NET lifecycle. Every time a postback happens (for example, when btnSortUp or btnSortDown is clicked), a new instance of your page is created, i.e., S_Sort is reinitialized to Desc. If you want to persist the value between postbacks, you can store it in the viewstate, for example, by encapsulating it in a private property:
private SortDirection S_Sort {
get { return (SortDirection)(ViewState["S_Sort"] ?? SortDirection.Desc); }
set { ViewState["S_Sort"] = value; }
}

Resources