Setting Scriptmanager and Update panel programmatically - c#

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.

Related

Gtk# OnButtonPressEvent is not working

I am pretty new to Gtk#, so please be patient. I want to create my own widget which will have two functions: draw what you want, and report if user clicked at this widget.
My code in method:
protected override bool OnExposeEvent (Gdk.EventExpose args)
{
// this works!
}
is fully working, but the code in following method:
protected override bool OnButtonPressEvent (Gdk.EventButton ev)
{
// this is not working at all :-(
Console.WriteLine("test!");
return base.OnButtonPressEvent (ev);
}
is not working. Both methods are some responses on events, so why OnButtonPressEvent is not called when I click at widget??
I have another widget which inherits from Table, but Buttons in Table works well, so why is there this problem?
Thanks in advance...
You have to create a signal to your button like this:
Button button = new Button();
button.Clicked += Button_Clicked;
private void Button_Clicked(object sender, EventArgs e)
{
//some stuff
}
It will work correctly. This is the way you must use Button widget. Sometimes it's depending how you layout your app, some widgets can be on your button and it can make unwanted behaviors.
You want to use Button Clicked signal instead of Button Pressed. That's what I use with all of my buttons and it works as expected.

How to Refresh GridView after closing down Dialog in ASP .NET

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
}

Make a .net WebForm dynamic without autopostback

Here's my issue, I'm using visual studios 2010 and I'm trying to create a form to fill then send it. This form requires a fileupload and some informations. The problem is that when I want to show/hide some pannel or textbox I can't do it dynamicly and I have to use autopost back which unselect the file selected in the fileupload control :
protected void CB_image_CheckedChanged(object sender, EventArgs e)
{
if (CB_image.Checked)
{
PanelImage.Visible = true;
}
else
{
PanelImage.Visible = false;
}
}
This is an exemple of code i'd like to pull out without using autopostback property on my CB_image (CB = checkbox). Any help would be appreciated, thanks
You can't do that. Something has to trigger a postback so the code on the server-side can check whether the checkbox is checked or not, and act according to it.
You can use a javascript timer that initiates a postback and work that way, I wouldn't do that though.
btw,
protected void CB_image_CheckedChanged(object sender, EventArgs e)
{
PanelImage.Visible = CB_image.Checked;
}
looks much cleaner!

Problem in setting Checked property of radio button

I have designed a gridview with a radio button inside the itemtemplet. I also have a confirm button. A user is allowed to check several radio button and when a confirm button is pressed then database is updated with the value of radio button.
But when i check the button it is shown as checked in UI but when I check through code behind, it shows checked property of radio button as false.
for (int i = 0; i < gvTransaction.Rows.Count; i++)
{
if (!((String.IsNullOrEmpty(gvTransaction.Rows[i].Cells[0].Text)) || (gvTransaction.Rows[i].Cells[0].Text == " ")))
{
string transactionID = gvTransaction.Rows[i].Cells[0].Text;
RadioButton btn = (RadioButton)gvTransaction.Rows[i].FindControl("rbtSelect");
if (btn.Checked)
{
// Although the radiobutton is checked code never reach here.
}
}
}
If this is Winforms, my guess would be because the edited Radio button cell value is not committed until it's validated, which happens when the cell lose focus. If you want to commit the modifications immediately, you can handle the CurrentCellDirtyStateChanged event, and call the CommitEdit method in the handler like this:
void gvTransaction_CurrentCellDirtyStateChanged(object sender, EventArgs e)
{
if (gvTransaction.IsCurrentCellDirty)
{
gvTransaction.CommitEdit(DataGridViewDataErrorContexts.Commit);
}
}
Use this if the radiobutton.checkedchanged event.
How are you binding the GridView? If you are doing it in Page Load, make sure that you are not reloading the gridView on page PostBacks
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //Bind GridView here }
do let us know if this help

How can I create buttons and hook up events from postback

I need to generate buttons initially based on quite a processor and disk intensive search. Each button will represent a selection and trigger a postback. My issue is that the postback does not trigger the command b_Command. I guess because the original buttons have not been re-created. I cannot affort to execute the original search in the postback to re-create the buttons so I would like to generate the required button from the postback info.
How and where shoud I be doing this? Should I be doing it before Page_Load for example? How can I re-construct the CommandEventHandler from the postback - if at all?
namespace CloudNavigation
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
// how can I re-generate the button and hook up the event here
// without executing heavy search 1
}
else
{
// Execute heavy search 1 to generate buttons
Button b = new Button();
b.Text = "Selection 1";
b.Command += new CommandEventHandler(b_Command);
Panel1.Controls.Add(b);
}
}
void b_Command(object sender, CommandEventArgs e)
{
// Execute heavy search 2 to generate new buttons
Button b2 = new Button();
b2.Text = "Selection 2";
b2.Command += new CommandEventHandler(b_Command);
Panel1.Controls.Add(b2);
}
}
}
The b_Command Event Handler method is not being executed because on post back buttons are not being recreated (since they are dynamically generated). You need to re-create them every time your page gets recreated but in order to do this you need to explicitly cache information somewhere in state.
If this a page-scoped operation easiest way is to store it in the ViewState (as strings - if you start loading the ViewState with objects you'll see performance go down) so that you can check it on next load (or any other previous event) and re-create buttons when reloading the page.
If the operation is session-scoped, you can easily store an object (array or whatever) in session and retrieve it on next Load (or Init) to re-create your controls.
This scenario means that you need just to store some info about your button in your b_Command EventHandler instead of creating and adding buttons since if you do so you'll lose relative information in the next postback (as it is happening now).
so your code would become something like:
namespace CloudNavigation
{
public partial class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
this.recreateButtons();
}
else
{
// Execute heavy search 1 to generate buttons
Button b = new Button();
b.Text = "Selection 1";
b.Command += new CommandEventHandler(b_Command);
Panel1.Controls.Add(b);
//store this stuff in ViewState for the very first time
}
}
void b_Command(object sender, CommandEventArgs e)
{
//Execute heavy search 2 to generate new buttons
//TODO: store data into ViewState or Session
//and maybe create some new buttons
}
void recreateButtons()
{
//retrieve data from ViewState or Session and create all the buttons
//wiring them up to eventHandler
}
}
}
If you don't want to call recreateButtons on page load you can do it on PreLoad or on Init events, I don't see a difference since you'll be able to access ViewState/Session variables everywhere (on Init viewstate is not applied but you can access it to re-create your dynamic buttons).
Someone will hate this solution but as far as I know the only way to retain state data server-side is ViewState - Session - Page.Transfer or client-side cookies.
The buttons need to be created before the load event, or state won't be wired up correctly. Re-create your buttons in Init() instead.
As for how to do this without re-running the search, I suggest you cache the results somewhere. The existence of a result set in the cache is how your button code in the Init() event will know it needs to run.
Alternatively, you could place the buttons on the page statically. Just put enough there to handle whatever the search returns. If you're thinking that maybe that would be way too many items, then ask your self this: will your users really want to sort through that many items? Maybe you should consider paging this data, in which case static buttons aren't as big a deal any more.
What happens when the postback event handling tries to find the control it dosen't exists on the collection.
Checkout Denis DynamicControlsPlaceholder # http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx
Hope it helps
Bruno Figueiredo
http://www.brunofigueiredo.com
Does your ASPX have the event handler wired up?
<asp:Button id="btnCommand" runat="server" onClick="b_Command" text="Submit" />
I agree with Joel about caching the search results. As for the buttons you can create them dynamically at the init or load phases of the page lifecycle but be aware that if you remove a button and then add it back programmatically you will mess up your state.
In one of my projects we have a dynamic form that generates field son the fly and the way we make it work is through an array that is stored in the cache or in the viewstate for the page. The array contains the buttons to display and on each page load it re-creates the buttons so that state can be loaded properly into them. Then if I need more buttons or a whole new set I flag the hide value in the array and add a new set of values in the array for the new set of corresponding buttons. This way state is not lost and the buttons continue to work.
You also need to ensure that you add a handler for the on_click event for your buttons if you create them programmatically which I think I see in your code up at the top.
Here is a sample with custom viewstate handling (note that buttons have EnableViewState = false):
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Execute heavy search 1 to generate buttons
ButtonTexts = new ButtonState[] {
new ButtonState() { ID = "Btn1", Text = "Selection 1" }
};
}
AddButtons();
}
void b_Command(object sender, CommandEventArgs e)
{
TextBox1.Text = ((Button)sender).Text;
// Execute heavy search 2 to generate new buttons
ButtonTexts = new ButtonState[] {
new ButtonState() { ID = "Btn1", Text = "Selection 1" },
new ButtonState() { ID = "Btn2", Text = "Selection 2" }
};
AddButtons();
}
private void AddButtons()
{
Panel1.Controls.Clear();
foreach (ButtonState buttonState in this.ButtonTexts)
{
Button b = new Button();
b.EnableViewState = false;
b.ID = buttonState.ID;
b.Text = buttonState.Text;
b.Command += new CommandEventHandler(b_Command);
Panel1.Controls.Add(b);
}
}
private ButtonState[] ButtonTexts
{
get
{
ButtonState[] list = ViewState["ButtonTexts"] as ButtonState[];
if (list == null)
ButtonTexts = new ButtonState[0];
return list;
}
set { ViewState["ButtonTexts"] = value; }
}
[Serializable]
class ButtonState
{
public string ID { get; set; }
public string Text { get; set; }
}

Categories