public void Submit1_Click(Object sender, EventArgs E)
{
string strheadlinesid1 = string.Empty;
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["folder"].ToString())))
{
strheadlinesid1 = Request.QueryString["folder"].ToString();
}
}
string URL = "view4.aspx?value="+strheadlinesid1;
Response.Redirect(URL);
}
This is a function I'm using but when I pass strheadlinesid1, "value" variable in URL is getting nothing.Why Is it happening.Is there any wrong in this approach.
On the button click, the page will be in the postback state and your condition will be false if (!IsPostBack) so it will not enter into the block and hence the value will not be assigned to strheadlinesid1
this block
if (!IsPostBack)
{
if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["folder"].ToString())))
{
strheadlinesid1 = Request.QueryString["folder"].ToString();
}
}
should be
if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["folder"].ToString())))
{
strheadlinesid1 = Request.QueryString["folder"].ToString();
}
A button click is a postback so strheadlinesid1 will not be assigned to and will be empty.
You need to remove the check for IsPostback, since button click events will usually be executed as postback events.
public void Submit1_Click(Object sender, EventArgs E)
{
string strheadlinesid1 = string.Empty;
if (!string.IsNullOrEmpty(Convert.ToString(Request.QueryString["folder"].ToString())))
{
strheadlinesid1 = Request.QueryString["folder"].ToString();
}
string URL = "view4.aspx?value="+strheadlinesid1;
Response.Redirect(URL);
}
Because Button click is a postback and the code to read "folder" query string doesn't execute if it is a postback?
So the value of "value" is always empty.
Remove the check for !IsPostback if that is what you wanted.
Related
Well, I have this situation, in a program I put a Button whose code is activated with PerformClick (programmatically), that button must be invisible in the interface so I put the value visible=false since the beginning of the program but the action on the event click doesn't perform, but if I put visible = true, the action actually is performed, any ideas of the problem?
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
_btnRecargarInsumos.PerformClick();
}
this.Close();
}
_btnRecargarInsumos: is the button and is actually performed in another Form.
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
cbACDescripcion: Combobox which will be "reloaded" with the values of the DataSet: dsDescripciones.
The property visible is false since the beginnig of the program, but I also try to set visible=true and just before the method PerformClick() change it, but is the same.
But if I put visible=true since the beginning it works in that way.
If you click a button that's not visible or not enabled, nothing happens, even if you click it programmatically. Here's a workaround that works for me, although it's a bit of a hack:
_btnRecargarInsumos.SuspendLayout();
_btnRecargarInsumos.Visible = true;
_btnRecargarInsumos.PerformClick();
_btnRecargarInsumos.Visible = false;
_btnRecargarInsumos.ResumeLayout();
Why not just put your code in a separate method?
Example:
private StuffToDoAtClick()
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
//Your Button.Click() code//
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
StuffToDoAtClick()
}
//Your Datagridview code//
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
StuffToDoAtClick();
}
this.Close();
}
I have the following code that opens a hyperlink in a different frame from code behind using the function ClientScript.RegisterStartupScript. The hyperlink was priory retrieved from a database and assigned to a label.
public void OpenWindow()
{
Formview_CurrentSelectedProcess.DataBind();
string url = (Formview_CurrentSelectedProcess.FindControl("LabelLink") as Label).Text;
string s = "window.open('" + url + "', 'main');";
Test.Text = s;
ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
}
This works perfect. Then I implemented a Querystring on that page. The Request Parameter is passed correctly and the hyperlink is opened on Pageload the way I expected. BUT: the next time this method is called, the hyperlink that will be opened, is the one belonging to the record specified in the Querystring Parameter. I placed a label inside to check if the correct parameter 's' is passed to ClientScript.RegisterStartupScript() and it is!!!
The mistake occurs with that function in case that the page had been loaded with a Querystring Parameter (eg.aspx?ID=324) Loading that same page without that parameter works perfect.
What happens? Why is ClientScript.RegisterStartupScript returning the old result, although it's input parameter has changed?
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateRootLevel();
string GetID = Request.QueryString["ID"];
if (String.IsNullOrEmpty(GetID))
{
}
else
{
InputNodeToOpen.Text = GetID;
ButtonClick_transmit(button1, EventArgs.Empty);
InputNodeToOpen.Text ="";
IDNodesToBeOpened.Text = "";
}
}
Any hints on that?
Martin
OK. Found the mistake:
After placing all actions in the page_load method inside separate brackets it works. I thought they were already assigned to a no-post-back situation; but they were NOT. The brackets are needed. So it must be:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
PopulateRootLevel();
string GetStructure = Request.QueryString["Structure"];
if (String.IsNullOrEmpty(GetStructure))
{
}
else
{
InputNodeToOpen.Text = GetStructure;
ButtonClick_transmit(button1, EventArgs.Empty);
InputNodeToOpen.Text = "";
IDNodesToBeOpened.Text = "";
}
}
}
I have session variable that dont update with new value. I have two pages, one were you enter the values and klick on the button and you get redirected to page 2 and there you can check your input, if this is wrong you click back-button and you go back to the first page where you can change the input but now when i click the button to validate again the new value does not show up in the session variable but only the old value. I have been readingabout session for the last day but i just cant find the problem, the behind code is below:
Page1
protected void Page_Load(object sender, EventArgs e)
{
if (this.Session["value1"] != null)
{
lbl1.Text = (String)this.Session["value1"].ToString();
}
}
public string info { get { return lbl1.Text; } }
protected void inputButton_onclick(object sender, EventArgs e)
{
Page.Validate();
if (Page.IsValid)
{
Session["value1"] = info;
Response.Redirect("~/validpage.aspx");
}
}
Page 2
protected void Page_Load(object sender, EventArgs e)
{
if (Session["value1"] != null)
{
lbl2.Text = (String)Session["value1"].ToString();
}
}
protected void BackButton_Click(object sender, EventArgs e)
{
Session["value1"] = lbl2.Text;
Response.Redirect("~/Default.aspx");
}
Maybe i have staired my self blind on this code as to me this should not have this problem it is presenting. Any idea and help will be appreciated.
Every time Page1 loads, lbl1 is set to the contents of the session, unless it's never been set. So when you click the button, the lbl1 is first set back to the content of the session as the page is loaded. You then read this value back & but it back in the session.
try this instead:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostback)
{
if (this.Session["value1"] != null)
{
lbl1.Text = (String)this.Session["value1"].ToString();
}
}
}
This says only set the value if we're not postback, ie NOT clicking a button on the page.
Also in page2, there's no need to call ToString AND cast to a string. Do either, not both.
I am trying to assign a ViewState value in my application with a SelectedIndexChanged function. Once it's assigned the postback will use the value to change some data and then set the value to zero but I can't seem to get it to work correctly. The controls are all created dynamically on Page_Load.
Page Load
protected void Page_Load(object sender, EventArgs e)
{
CreateAttributeControls();
TempProductVariantId = 0;
}
Create Attribute Controls
public void CreateAttributeControls()
{
...
var ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
ddlArtistArtworks.AutoPostBack = true;
...
}
ArtistArtwork_SelectedIndexChange
protected void ArtistArtwork_SelectedIndexChange(object sender, EventArgs e)
{
DropDownList ddl = sender as DropDownList;
TempProductVariantId = int.Parse(ddl.SelectedValue);
}
TempProductVariantId ViewState Save
public int TempProductVariantId
{
get
{
if (ViewState["TempProductVariantId"] == null)
return 0;
else
return (int)ViewState["TempProductVariantId"];
}
set
{
ViewState["TempProductVariantId"] = value;
}
}
When I load the page everything is fine. I change the DropDownList's value, It posts back, and the value is not set. Change it again the value is set and continues to change as I change the value of the DropDownList.
Any guidance on this would be greatly appreciated.
Note: I have tried changing when CreateAttributeControls() is called. In OnPreRender for example. I was given this to understand the lifecycle of the page Life Cycle
That's because you are essentially recreating the dropdown on every postback..
try this
public void CreateAttributeControls()
{
...
DropDownList ddlArtistArtworks;
if (!IsPostBack)
{
ddlArtistArtworks = new DropDownList();
ddlArtistArtworks.ID = "ddlArtistArtworksTest";
divAttribute.Controls.Add(ddlArtistArtworks);
ddlArtistArtworks.Items.Clear();
ddlArtistArtworks.AutoPostBack = true;
}
else
{
ddlArtistArtworks = (DropDownLise)divAttribute.FindControl("ddlArtistArtworksTest");
}
ddlArtistArtworks.SelectedIndexChanged += new EventHandler(ArtistArtwork_SelectedIndexChange);
...
}
For dynamically added controls, the event handler has to be linked up everytime so that has to be done outside the if-block, unconditionally.
Inside asp.net form I have few dynamically generated buttons, all of this buttons submit a form, is there a way to get which button was submit the form in page load event?
The sender argument to the handler contains a reference to the control which raised the event.
private void MyClickEventHandler(object sender, EventArgs e)
{
Button theButton = (Button)sender;
...
}
Edit: Wait, in the Load event? That's a little tricker. One thing I can think of is this: The Request's Form collection will contain a key/value for the submitting button, but not for the others. So you can do something like:
protected void Page_Load(object sender, EventArgs e)
{
Button theButton = null;
if (Request.Form.AllKeys.Contains("button1"))
theButton = button1;
else if (Request.Form.AllKeys.Contains("button2"))
theButton = button2;
...
}
Not very elegant, but you get the idea..
protected void Page_Load(object sender, EventArgs e) {
string id = "";
foreach (string key in Request.Params.AllKeys) {
if (!String.IsNullOrEmpty(Request.Params[key]) && Request.Params[key].Equals("Click"))
id = key;
}
if (!String.IsNullOrEmpty(id)) {
Control myControl = FindControl(id);
// Some code with myControl
}
}
This won't work if your code is inside a user control:
Request.Form.AllKeys.Contains("btnSave") ...
Instead you can try this:
if (Request.Form.AllKeys.Where(p => p.Contains("btnSave")).Count() > 0)
{
// btnSave was clicked, your logic here
}
You could try:
if (this.Page.Request.Form[this.btnSave.ClientID.Replace("_", "$")] != null) {
}
please try this code in page load event
string eventtriggeredCategory = Request.Form["ctl00$ContentPlaceHolder1$ddlCategory"];
if eventtriggeredCategory is returning any value its fired the event of ddlCategory
this is works fine for me
Thanks
Jidhu
Request.Form["__EVENTTARGET"] will give you the button that fired the postback
Use CommandArgument property to determine which button submits the form.
Edit : I just realized, you said you need this at PageLoad, this works only for Click server side event, not for PageLoad.