Adding or attaching an OnItemCommand Event handler to ASP.NET CheckBox - asp.net

I wonder if it is possible to add or attach or extend the asp.net checkbox in a way to add OnCommand , CommandName, CommandArgument functionality.
In brief I want to add to a asp. net CheckBox the OnCommand functionality like Button or LinkButton has.
I want to place a check box in a Repeater ItemTemplate and want to handle on checkbox clicks in OnItemCommand handler.

I solved my problem using a simple trick. I created a web user control using a checkbox and invisible link button.
<asp:CheckBox ID="cbSelector" runat="server" AutoPostBack="true" />
<asp:LinkButton ID="btnHiddenCheckBox" runat="server" Visible="false" CommandName="Select"></asp:LinkButton>
in the code behind i added some event handlers ...
protected void Page_Load(object sender, EventArgs e)
{
cbSelector.CheckedChanged += new EventHandler(cbSelector_CheckedChanged);
}
void cbSelector_CheckedChanged(object sender, EventArgs e)
{
btnHiddenCheckBox.CommandName = "Select";
btnHiddenCheckBox.CommandArgument = Convert.ToString(cbSelector.Checked);
((IPostBackEventHandler)btnHiddenCheckBox).RaisePostBackEvent(null);
}
and programmatically raised a Click event with parameters and it works.

Patrik I am going to suggest you something completely different, use JQuery and get this done via Ajax so to give the users way smoother experience.
there is a nice article with a step by step here: ASP.Net CheckBox and JQuery
everything is better than a CheckBox or a Button with AutoPostBack set to true nowadays in year 2011, 2012 soon ;-)

The same could be achieved using OnCheckChanged event.
You can find out what the RepeaterItem is to be able to access controls in the same RepeaterItem
ALternatively, see this SO question for a solution on how to use OnItemCommand for CheckBox:
Checkbox OnClick/ItemCommand in Repeater or DataList

Related

Dynamic ASP Form Post - Without Using an Iframe

I need to modify a 3-level dropdown <select> menu to function directly on the page and not inside an iframe (that's how we currently do it).
Here's how this menu works:
User makes a selection in 1st dropdown
Choices in 2nd dropdown are filtered based on what's chosen in 1st one; user selects from one of these choices
Choices in 3rd dropdown are filtered based on what's chosen in 2nd one; user selects from one of these choices; this last dropdown submits the form and redirects the user to another page, also passing the values from all three dropdowns.
Right now, this is accomplished using an <iframe> that queries an ASP.net database and reloads itself after each selection.
I really need it to work without using an iframe. Not sure what the most elegant way to approach this is ...
If you're open to using the ASP.NET Ajax Toolkit, they have a cascading dropdown control which does what you ask. One additional benefit, because it uses Ajax it does not have to reload the page after each selection in the dropdowns.
i think you'll probably need to use the 'onSelectIndexChanged` even on the DropDownList control. something like...
<asp:DropDownList id="ddl1" runat="server" OnSelectedIndexChanged="ddl1_OnSelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList id="ddl2" runat="server" OnSelectedIndexChanged="ddl2_OnSelectedIndexChanged"></asp:DropDownList>
<asp:DropDownList id="ddl3" runat="server" OnSelectedIndexChanged="ddl3_OnSelectedIndexChanged"></asp:DropDownList>
Page_Load()
{
if(!IsPostBack)
{
ddl1.DataSource = getdata();
ddl1.DataBind();
}
protected void ddl1_onSelectedIndexChanged(object sender, EventArgs e)
{
ddl2.DataSource = getData(ddl1.SelectedValue);
ddl2.DataBind()
}
protected void ddl1_onSelectedIndexChanged(object sender, EventArgs e)
{
ddl3.DataSource = getData(ddl2.SelectedValue);
ddl3.DataBind()
}
protected void ddl3_onSelectedIndexChanged(object sender, EventArgs e)
{
Response.Redirect("SomePage.aspx?ddl1="+ddl1.SelectedValue+"&ddl2="+ddl2.SelectedValue+"&ddl3="+ddl3.SelectedValue, true);
}
You could put all 3 of them in an UpdatePanel.
I am sure your changes are already making the page refresh.
Putting them in an update panel will make only the panel refresh, causing a partial page update.
This uses ajax to do the job internally.
Here is a update panel example, if you want one.
P.s.: Asp.net's method to create a <select> is a DropDownList control. I hope you are already using those, if not convert your selects to a dropdownlists.

Issue with getting control id when tabcontainer is used

I have an AJAX Control Toolkit TabContainer in my ASP.NET page. In one of the tabs in the TabContainer, there is a GridView. Now, I have an UpdatePanel for which I want to give trigger as "RowCommand" of GridView mentioned above. The UpdatePanel is outside the TabContainer. But when I give the GridView id, I am getting the error:
A control with ID 'grvSummary' could not be found for the trigger in UpdatePanel 'updSegment'.
Trigger markup:
<asp:AsyncPostBackTrigger ControlID="grvSummary" EventName="RowCommand" />
You will have to programmatically add the trigger to your UpdatePanel. This is because your GridView may or may not start in a different ContentPlaceHolder than the UpdatePanel (namely, the <ContentTemplate> of your TabContainer). Like this (note that this really needs to be done in Page_Init, due to the Page Life Cycle):
protected void Page_Init(object sender, EventArgs e)
{
AsyncPostBackTrigger trig = new AsyncPostBackTrigger();
trig.ControlID = grvSummary.UniqueID;
trig.EventName = "RowCommand";
updSegment.Triggers.Add(trig);
}
It looks like this may be a problem with ASP.NET / AJAX assuming the wrong Control.UniqueID value for the Control being used as a trigger.
Source: Triggering an UpdatePanel in a different ContentPlaceHolder

ASPxGridView PerformCallback() does full page postback

I have a checkbox that when clicked, calls a javascript program that calls grid.PerformCallback(), where grid is the client instance name of my ASPxGridView gridview. This gridview also has a custom callback method which databinds the table. However when i click my checkbox, instead of only performing callback on the gridview, my page does a full postback, which posts the form. How do i make it so that it only updates the gridview?
function toggle()
{
productGrid.PerformCallback();
}//end toggleExch()
<dx:ASPxGridView ClientInstanceName="productGrid" Width="100%" ID="productGrid" runat="server"
DataSourceID="ProductSQL" EnableCallBacks="true" OnCustomCallback="productGrid_OnCustomCallback">
</dx:ASPxGridView>
protected void productGrid_OnCustomCallback(object sender,
DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
{
System.Diagnostics.Debug.WriteLine("in postback");
productGrid.DataBind();
}//end productGrid_OnCustomCallback()
so basically the debug line is not printed and the page goes into full postback - how do i only postback and databind the grid? (i need to do more server side processing before databinding or directly binding from jquery is out of the question)
found the answer - should use iscallback instead of ispostback
Unfortunately, you did not post the aspx markup of the button. However, if this is the ASPxButton, make certain that its AutoPostback property is false ...

Repeater databound loses data & event on postback - is there a best practice solution?

Currently struggling with a problem that I've encountered variations on in the past. At the moment a worthwhile solution escapes me, but it seems such an obvious issue that I can't help wondering whether or not there's a "best practice" approach I should adopt.
Without code, here's the issues in a nutshell:
page has databound control (a repeater) which isn't populated until user inputs data and clicks a button.
Repeater item template contains a button
User clicks button, page posts back. On load, the repeater is actually empty so event is never handled because the originating control no longer exists
Go back to the beginning of wretched cycle
I've confirmed that this is the problem because if you provide the repeater with some static data on page load, everything works fine. But of course that's no use because it has to be populated dynamically.
Is there a commonly approved way round this headache? I can store the data in session and re-use it on page load, but it seems terribly clumsy.
Cheers,
Matt
If the event is being fired by a button within a repeater then this would bubble up to the repeaters ItemCommand event. Using a buttons CommandName and CommandArgument parameters you can then identify which button was clicked and act accordingly. Below is some basic markup and code behind to demonstrate the approach:
HTML:
<asp:Repeater ID="rptTest" runat="server" onitemcommand="rptTest_ItemCommand"
onitemdatabound="rptTest_ItemDataBound">
<ItemTemplate>
<p>
<asp:Button ID="btnTest" runat="server" />
</p>
</ItemTemplate>
</asp:Repeater>
<asp:Button ID="btnLoad" runat="server" Text="Load" onclick="btnLoad_Click" />
Code behind events:
protected void rptTest_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
Button button = (Button)e.Item.FindControl("btnTest");
button.Text = string.Format("Button {0}", e.Item.DataItem.ToString());
button.CommandName = e.Item.ItemIndex.ToString();
}
protected void rptTest_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Response.Write(string.Format("Postback from button {0}", e.CommandName));
}
protected void btnLoad_Click(object sender, EventArgs e)
{
List<int> list = new List<int>();
list.Add(1);
list.Add(2);
list.Add(3);
list.Add(4);
rptTest.DataSource = list;
rptTest.DataBind();
}
Hopefully i've understood the problem and this helps.
If any of your controls are created dynamically, then they have to be recreated during post back in order for the events etc to get hooked back up.
If this is the case, take a look at a control built by a guy named Denis Bauer. We use this with just some slight modifications and it's perfect.

asp.net :how to use eventargs for parameter passing on button onclick?

i have seen some code in which people pass parameter through commandParameter Property of web control.then what is use of eventargs.
This can be useful if you have the same EventHandler method for different buttons. Example, say your markup looks like this:
<asp:Button ID="button1" runat="server" CommandArgument="MyVal1"
CommandName="ThisBtnClick" OnClick="MyBtnHandler" />
<asp:Button ID="button2" runat="server" CommandArgument="MyVal2"
CommandName="ThatBtnClick" OnClick="MyBtnHandler" />
You can have the same event handler for both buttons and differentiate based on the CommandName:
protected void MyBtnHandler(Object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.CommandName)
{
case "ThisBtnClick":
DoWhatever(btn.CommandArgument.ToString());
break;
case "ThatBtnClick":
DoSomethingElse(btn.CommandArgument.ToString());
break;
}
}
Source: aspnet-passing-parameters-in-button-click-handler
Various Button type controls in .NET have an OnCommand event as well as an OnClick event. When using the OnCommand event you have additional parameters you can apply to the Button such as CommandName and CommandArgument. These can then be accessed in the CommandEventArgs.
It is useful in places where you want to assign the same method to multiple buttons and use the CommandName and CommandArgument parameters to indicate what functionality clicking that button will produce.
The EventArgs class is a base class. Other web methods use other Event args.
e.g. a LinkButton has an OnClick event with a default EventArgs parameter, but it also has an OnCommand event that takes a CommandEventArgs : EventArgs parameter which has some extra information (namely CommandName & CommandArgument).
The event args base class is just there as a place holder so that all the EventHandlers conform to a similar signature.

Resources