I have a main page with a Gridview that shows data from a Database. In the gridview there is a button, when this button is clicked a Dialog will appear allowing u to edit the selected row. The dialog is created as an aspx class. When i edit the data and close down the dialog i want to refresh my GridView on the Main page so the edited data is represented. How can i do this?
my code for editing the data and closing down the dialog is this:
protected void editButton_Click(object sender, EventArgs e)
{
string alias = Request.QueryString["alias"];
string custid = Request.QueryString["custid"];
controller.EditDeliveries(custid, alias, tdaysField.Text, thoursField.Text, ttypeField.Text, pdaysField.Text, phoursField.Text, ptypeField.Text);
ClientScript.RegisterClientScriptBlock(GetType(), "onUpload", "<script type='text/javascript'> window.close();</script>");
}
Can anyone help ?
And if u need to see more code please tell me.
Just set your datasource again and rebind it in the postback
gvMyGrid.DataSource = myData; //Fresh from the database
gvMyGrid.DataBind(); //Bam, fresh data
Edit: Oh and if it's another Control that is the source of the postback, you can use a Bubble Event to trigger the refresh.
Second Edit: To have the Dialog Page tell the Main Page to update that grid:
RaiseBubbleEvent(this, new CommandEventArgs("DataUpdated", "This could be null, or I could be a message to let the user know things are updated"));
Then on your main Page
protected override bool OnBubbleEvent(object sender, EventArgs e)
{
if (e is CommandEventArgs)
{
var args = (CommandEventArgs)e;
//Could use args.CommandArgument here
switch(args.CommandName)
{
case "DataUpdated":
gvMyGrid.DataSource = myData;
gvMyGrid.DataBind();
return true; //Handled the event LIKE A BOSS
}
}
return false; //I didn't handle this event
}
Related
I am making a web page with asp.net, the problem has to be with a asp:datagrid and its page index changing event. The records are displaying and paging correctly but after making a filter and reduce its grid content and trigger the page index again, the grid refreshes and came back all the other records.
This is my PageLoad()
if (!IsPostBack)
{
txtDate1.Attributes.Add("placeholder", "Enter Start Date DD//MM//YY");
txtDate2.Attributes.Add("placeholder", "Enter End Date DD//MM//YY");
this.bindgrid();
}
else
this.bindgridfilter(txtDate1.Text, txtDate2.Text);
My bindGridsmethods()
private void bindgrid()
{
ds = Classes.DBMethods.ShowRecords();
grdRecords.DataSource = ds.Tables[0];
grdRecords.DataBind();
}
private void bindgridfilter(string date1, string date2)
{
dsf = Classes.DBMethods.SearchBetweenDates(date1,date2);
grdRecords.DataSource = dsf.Tables[0];
grdRecords.DataBind();
}
My PageIndexChanging Event
protected void grdRecords_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdRecords.PageIndex = e.NewPageIndex;
grdRecords.DataBind();
}
And my FilterButton Click()
protected void Filter_Click(object sender, EventArgs e)
{
grdRecords.DataSource = null;
bindgridfilter(txtDate1.Text, txtDate2.Text);
}
Now it does the opposite it disappear the grid after making a change of page without a filter, and if it applies a filter now the pagination works correctly
In most web pages, just setting up a drop down list, a grid view, or whatever?
You ONLY want to bind and load the drop down list, or gridview ONE time.
If you re-beind, and re-load the control AGAIN with data, then you will lose your settings.
So, 99% of the time, you want to do such loading ONLY one time,and ONLY on the first page load. Hence, your page load event needs to be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtDate1.Attributes.Add("placeholder", "Enter Start Date DD//MM//YY");
txtDate2.Attributes.Add("placeholder", "Enter End Date DD//MM//YY");
bindgrid();
}
}
So, now when a button is clicked, or say gv selected index changes, or whatever? Then the information can be used.
If you rebind on each post back, then a gv, or even a simple drop down list will lose its values - hence ONLY load and bind your controls on first "real" page load as per above.
Page load fires EACH time and for each post back. So, any button click or whatever? The page load event will run again. Hence, your general setup code, startup code, and code to load up things like a drop down list, or gridview?
You need to have a if (!IsPostBack) code stub. The last 200 web pages I have created - everyone has such a code stub, and you really for all purposes can't build a working web page without this "setup" or "first load" code stub. If you re-load or re-setup your controls each time, then anything the user enters or does with such controls will be lost.
Of course YOUR buttons etc. and code can and will re-load the grid, but since the page load fires every time with all and any button click (any post-back), then that re-load code will also re-fire, and you don't want that to occur.
Is it correct? PageLoad()
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
txtDate1.Attributes.Add("placeholder", "Enter Start Date DD//MM//YY");
txtDate2.Attributes.Add("placeholder", "Enter End Date DD//MM//YY");
this.bindgrid();
}
}
PageIndex Event
protected void grdRecords_PageIndexChanging1(object sender, GridViewPageEventArgs e)
{
grdRecords.PageIndex = e.NewPageIndex;
if (txtDate1.Text == "" & txtDate2.Text == "")
{
bindgrid();
}
else
this.bindgridfilter(txtDate1.Text, txtDate2.Text);
}
I'm working on Visual Studio, in C# language
I would like to ask if there's someway to go back to get the ButtonX.Text I pressed on the previous page, some sort of Login without a password.
Basically I need a worker to specify which person he is by clicking on their name (button) then it goes foward to the next page I have a label on the MasterPage but it resets everytime it goes on a next page what I would like to do is keep the info there
If you need some code tell me.
Thanks
You could use session variables?
On the button click handler on the first page...
protected void button_Click(object sender, EventArgs e)
{
Session["Worker"] = button.Text;
}
Then on the second page...
Label.Text = Session["Worker"];
Based-off your reply to Zollistic's answer, you could do this...
Apply this event to all your all your worker buttons...
protected void button_Click(object sender, EventArgs e)
{
if (Session["Worker"] == null) Session["Worker"] = "";
Session["Worker"] += button.Text + ",";
}
Now Session["Worker"] has a character-delimited list of all the clicked buttons. The character in this example is a comma; but you can change it to whatever you want (i.e. a pipe "|").
I am using a same submit button to Add new row and also edit a row's values of a database table in and ASP.NET form. I use CommandArgument to change the method being applied.
protected void btnSave_Click(object sender, EventArgs e)
{
if (!CheckProfileComplete())
{
btnSave.CommandArgument = "new";
btnSave.Text = "Save";
}
else
{
btnSave.CommandArgument = "update";
btnSave.Text = "Update";
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
if (!Page.IsValid)
return;
string strCmd = this.btnSave.CommandArgument;
if (strCmd.ToLower() == "new")
{
//Add new Record
}
else if(strCmd.ToLower() == "update")
{
//Update existing record.
}
}
In case of Add New a blank form is presented, while in Update a form with previously added data to the database table is presented, where the user can edit the values and then save it. The CommandArgument is updated at page load, after checking some other parameters.
The Add New method runs fine, but I am unable to edit the values in case of Update. I mean the form posts the same values that were dynamically filled before presenting to the user for editing. No change is being shown after the Save (Update mode) button is clicked. It seems like the form takes its previous state as the current state even after values being changed and posted back.
for example if tbName.Text had value foo when read from database initially.
user edited it, now tbName.Text has bar before postback.
But when the form is posted while in debug mode, the tbName.Text shows foo as the current value in place of bar
You need to determine which event it is that occurs which makes you need to change the way the button works. Once you know that, then, inside that event you unsubscribe the button and subscribe it to something else.
private void OnMyEvent()
{
button1.Click -= "theEventIDoNotWant";
btnSave.Click += new EventHandler(theEventIWant);
}
From what i know it should work something like this:
<asp:LinkButton ID="btnSave" runat="server" CommandArgument="Add" OnCommand="btnSave_Click">Save</asp:LinkButton>
and code behind:
protected void btnSave_Click(object sender, CommandEventArgs e)
{
if(string.Equals(e.CommandArgument,"Add"))
{
//do save logic here
btnSave.CommandArgument = "Edit";
btnSave.Text = "Update";
}
if (string.Equals(e.CommandArgument, "Edit"))
{
//do update logic here
btnSave.Text = "Update done";
}
}
you need to implement only one event handler and check what argument you sent with the event using the CommandEventArgs.
Hope this helps you.
Cheers!
I want to change the text of the button with partial post backs. There a few things that i dont understand..
Button quote;
public void addButtonsPost()
{
quote=new Button();
quote.Click += quote_Click;
sm.RegisterAsyncPostBackControl(quote);
}
public void quote_Click(object sender, EventArgs e)
{
if (quote.Text == "quote")
{
quote.Text = "quote+";
}
else
{
quote.Text = "quote";
}
}
So basically, the text of the button should be executed each time and the text should change from quote to quote+ and vice versa. How do i achieve this...and do i need to use viewState to save the current button text between the partial postbacks or is it not necessary?
I think this post will help you with your problem.
How can I programmatically add triggers to an ASP.NET UpdatePanel?
Assuming your addButtonsPost call is working and registering the asycn post back, then you should just need to tell the update panel to refresh by calling UpdatePanel1.Update(). Please not UpdatePanel1 should be the Id of your update panel.
I have a problem with a webform.
My Goal: Intially when a page is loading, it has to load every textbox empty. After filling the info and click submit, it has to get submitted(UpdatePaymentInfo())
Problem: Here, When the user fills the info and clicks Submit,it calls onload function even before the submit button and makes all text box empty.
Here is the code:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
string QueryStringupdatecreditcard1 = Request.QueryString.ToString();
if (String.Equals(QueryStringupdatecreditcard1, "tabID=B"))
{
divTitle.Visible = false;
trmain.Visible = false;
tdOrderSummary.Visible = false;
trCCandBilling.Visible = true;
trtest2.Visible = false;
divUpdatecreditcard.Visible = true;
trusecompaddress.Visible = false;
txtFirstName.Text = "";
txtLastName.Text = "";
txtAddress1.Text = "";
txtAddress2.Text = "";
txtCity.Text = "";
txtZip.Text = "";
txtCardNo.Text = "";
txtVccNumber.Text = "";
trAmountCharged.Visible = false;
}
}
protected void imgbtnSubmit_Click(object sender, ImageClickEventArgs e)
{
try
{
UpdatePaymentInfo();
}
}
Wrap the current contents of your OnLoad method in:
if (!Page.IsPostBack)
{
// Code in here will only be executed when the page is *not* being loaded by a postback
}
This is because, as per the ASP.NET Page Life Cyle, the things that you care about in this instance happen in this order:
Load - During load, if the current request is a postback, control
properties are loaded with information
recovered from view state and control
state.
Postback event handling - If the request is a postback, control event
handlers are called. After that, the
Validate method of all validator
controls is called, which sets the
IsValid property of individual
validator controls and of the page.
So what happens is (somewhat simplified):
You click the image button, triggering the postback.
The data from your form is loaded into your controls.
Your OnLoad method overwrites the values in the controls to clear them.
Your click handler is run, but because of step 3 it sees empty values.
As others have sort-of mentioned, it wouldn't necessarily be a bad thing to refactor your OnLoad method whilst you're doing this. At the moment you seem to have it doing two distinct things:
Clearing the text fields
Setting the visibility of fields
It might be worth separating this into one or two (depending on if the visibility setting and field clearing will be done independently) separate methods and adjusting your OnLoad method so it looks like this:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (!Page.IsInPostBack)
{
SetFieldVisibility();
ClearFields();
}
}
Page_Load always occurs.
See the documentation on the Page Lifecycle
What you need to do is check to see if the Page_Load is being triggered by a Postback.
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
///do stuff in here that you want to occur only on the first lad.
}
else
}
// code that you want to execute only if this IS a postback here.
{
}
// do stuff you want to do on Page_Load regardless of postback here.
}
You can use the IsPostBack property of the Page as follows:
protected override void OnLoad(EventArgs e) {
if (!Page.IsPostBack) {
EmptyTextBoxes();
}
}
Have you tried wrapping the form reset code in a check to see if the page is a postback?
if(!Page.IsPostback) {
// Do form reset here
}
You thought about using the IsPostBack page variable?
protected override void OnLoad(EventArgs e)
{
if(!IsPostBack){
//all your logic here.
}
}
if it's the case, you might use a databound control and set it to insert mode