As topic , I have a item template than consist of a dropdownlist , when user attempt to click it , warning should come out to warn User whether to continue or not. after I click OK , nothing happens , it is not going back to the postback
My gridview code for item template is like the following :
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="cboStatus" runat="server" AutoPostBack="True">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
My Code behind will set the JavaScript to the attribute
DropDownList cboStatus= (DropDownList)e.Row.FindControl("cboStatus");
cboStatus.Attributes.Add("onChange", "Confirmation();");
JavaScript :
function Confirmation() {
if (confirm('Are you sure you want to do this?')) {
__doPostback(this, 'Select${0}');
}
}
I want my postback to call this function
protected void cboStatus_Click(object sender, EventArgs e)
{
//Some Code
}
Change your code a bit like below. This has been tested locally to be working fine. The below code will fire up the server event for selection changed if Ok from the confirmation window is clicked otherwise not.
Your gridview template definition
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:DropDownList ID="cboStatus" runat="server" AutoPostBack="true" onchange="return Confirmation ();" OnSelectedIndexChanged="cboStatus_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
JavaScript function for the onchange event
function Confirmation() {
if (confirm('Are you sure you want to do this?')) {
__doPostBack('__Page', '');
}
return false;
}
And finally your server event for SelectedIndexChanged
protected void cboStatus_SelectedIndexChanged(object sender, EventArgs e)
{
// Some code
}
Related
I have a requirement to do some logic execution upon change of dropdownlist value. Before executing the logic i need to take user confirmation and then call server side method to complete the process. Not sure How to call server side method based on modal popup confirmation response from user. So if user confirms with Yes button on the modal popup server side code should be called otherwise do nothing.
Here is the code i have . Server side does not get called upon modal popup confirmation.
function PopupDialog(title, text) {
var div = $('<div>').html('<br>'+text).dialog({
title: title,
modal: true,
height: 190,
width: 320,
buttons: {
"Yes": function () {
$(this).dialog('close');
},
"No": function () {
$(this).dialog('close');
}
}
});
return true;
};
<asp:GridView runat="server" ID="grdTransactions" SkinID="gridviewskin"
AllowSorting="true" AllowPaging="true" PageSize="30" Width="100%"
OnRowDataBound="grdTransactions_RowDataBound"
OnDataBound="grdTransactions_DataBound"
OnSelectedIndexChanged="grdTransactions_SelectedIndexChanged">
.............
<asp:TemplateField Visible="true" HeaderText="Status" >
<ItemTemplate>
<asp:Label runat="server" ID="lblStatus" Visible="False" Text='<%# ShowStatus( Container.DataItem ) %>' />
<asp:DropDownList ID="ddlTransactionList" AutoPostBack="True" OnSelectedIndexChanged="ddlTransactionList_SelectedIndexChanged" onchange="return PopupDialog('Remittance Confirmation','Are you sure you want to update the status?.');" runat="server"></asp:DropDownList>
<br/>
</ItemTemplate>
</asp:TemplateField>
The server side code is below:
protected void ddlTransactionList_SelectedIndexChanged(object sender,
EventArgs e)
{
//Your Code
if (OnDataChanged != null)
OnDataChanged(sender, e);
}
Check the generated HTML code of your page and have a closer look on your dropdown. It should look like this:
<select name="gridView$ctl02$ddlTransactionList" onchange="return PopupDialog('Remittance Confirmation','Are you sure you want to update the status?.');setTimeout('__doPostBack(\'gridView$ctl02$ddlTransactionList\',\'\')', 0)" id="gridView_ddlTransactionList_0">
The problem is that you "return" the outcome of your PopupDialog so that the __doPostback function (AutoPostBack) has no chance of getting called. My advice: Only return if the user rejects the change. If the user agrees dont return anything.
Edit (forgot post the solution code)
<asp:DropDownList ID="ddlTransactionList" AutoPostBack="True" OnSelectedIndexChanged="ddlTransactionList_SelectedIndexChanged" onchange="if(! PopupDialog('Remittance Confirmation','Are you sure you want to update the status?.')){return false;}" runat="server"></asp:DropDownList>
While, I want to trigger btnSearchSuiteGroup_Click event when pressing "enter" on txtSuiteGroupName which described in aspx, code below:
<asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD" onkeypress="return searchKeyPress(event)"></asp:TextBox>
<asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search" CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
<script type="text/javascript">
function searchKeyPress(e) {
// look for window.event in case event isn't passed in
if (typeof e == 'undefined' && window.event) { e = window.event; }
if (e.keyCode == 13) {
document.getElementById('<%=btnSearchSuiteGroup.ClientID%>').click();
}
}
</script>
While, the btnSearchSuiteGroup_Click is defined in the source cs file:
protected void btnSearchSuiteGroup_Click(object sender, EventArgs e)
{
this.LinqDataSource1.WhereParameters["SuiteGroupName"].DefaultValue = this.txtSuiteGroupName.Text;
this.GridView1.DataBind();
if (GridView1.Rows.Count == 0)
Response.Write("<script language='javascript'>window.alert('No record found!')</script>");
}
When I browse the website, the keypress on the textbox cannot initiate the button click event, anything wrong in the code?
If you use Panel you would not need to use any javascript function. You can specify default button Id for panel like below
<asp:Panel runat="server" DefaultButton="btnSearchSuiteGroup">
<asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD">
</asp:TextBox>
<asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search" CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
</asp:Button>
</asp:Panel>
OfCourse you can have Multiple panels on single page for assigning different default buttons for separate panels!
For More On Panel.DefaultButton Property
I suggest the following method:
1. Create a function 'ButtonClick' and put all the code in 'btnSearchSuiteGroup_Click' function into it.
2. Add 'onkeypress' event for the textbox 'txtSuiteGroupName' by looking at this thread
3. Whenever the above event is fired, see if the entered key is 'Enter'.
4. If the key is 'Enter' key, call the function 'ButtonClick'.
I have a DataList and inside it I have a DropDownList:
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
How can I get selectedindexchanged event of DropDownList on the server side? I tried this:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but it is not working.
You have defined the server side method:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but you have not told client side that there is an event for you, so in html code tell it like:
onselectedindexchanged="ddlitem_selectedindexchanged"
and also set AutoPostBack property to true.
From the SelectedIndexChanged event the easiest is to cast the sender to the DropDownList
var ddl = (DropDownList)sender;
The sender is always the control that is the source of the event.
For the sake of completeness, from ItemDataBound of the DataList:
protected void dlconfigureItem_ItemDataBound(object sender, DataListItemEventArgs e)
{
DropDownList ddlitem = e.Item.FindControl("ddlitem") as DropDownList;
if (ddlitem != null)
{
// ...
}
}
Edit: Have you forgotten to register the event?
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
Note that you should not bind your DataList to it's DataSource on postbacks, otherwise events are not triggered. So check for the IsPostBack property of the page.
For example in page_load:
if(!IsPostBack)BindDataList();
Register the event and set AutoPostBack="true"
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
AutoPostBack="true"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
event (on selected index change you can get the selected value)
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
var ddlList = (DropDownList)sender;
string selectedValue = ((DropDownList)ddlList.NamingContainer.FindControl("ddlitem")).SelectedValue;
}
Not sure if you can't get the selected item on the server or you can't find the way to handle the event. In case your problem is with the event handling, try this
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
AutoPostBack="true" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
In my repeater's ItemTemplate I've a CheckBox and a disabled TextBox, I need to implement this idea: TextBox only gets enabled if the the CheckBox is checked .. so I set the CheckBox AutoPostBack to true and I tried to put this code in ItemDataBound. but I can't find my control which is weird because I use the same code but in loop "MyRptr.Item[i].FindControl...." and it works! .. I don't want to loop through all the Items, I just wish If I can know the Item number or location in which the CheckBox was created. and I've also tried to create an event handles for the CheckBox's CheckedChanged event but I can't find the CheckBox either!
protected void MyRptr_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
CheckBox ChkBx = e.Item.FindControl("IsSelected_ChkBx") as CheckBox;
if (ChkBx.Checked == true)
{
TextBox TxtBx = e.Item.FindControl("Value_TxtBx") as TextBox;
TxtBx.Enabled = true;
}
}
<asp:Repeater ID="MyRptr" runat="server"
onitemdatabound="MyRptr_ItemDataBound">
<ItemTemplate>
<asp:CheckBox ID="IsSelected_ChkBx" runat="server" Text='<%# Eval("Item") %>' AutoPostBack="True" OnCheckedChanged="IsSelected_ChkBx_CheckedChanged" />
<asp:TextBox ID="Value_TxtBx" runat="server" Enabled="false"></asp:TextBox>
<asp:HiddenField ID="ID_HdnFld" runat="server" Value='<%# Eval("ID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<br></br>
</SeparatorTemplate>
</asp:Repeater>
So basically I need a clean and simple way to implement my logic and If I could get an explanation for what's happening it would be great, so any ideas =) ?
You can find your textbox as follow, but I think its better use the jQuery instead of server-side event
protected void IsSelected_ChkBx_CheckedChanged(object sender, EventArgs e)
{
var ch = (CheckBox)sender;
var txt = ch.Parent.FindControl("Value_TxtBx") as TextBox;
}
Ok, so I'm struggling with using asp:formview.
I've got the formview up and running and I've added the 'Edit' button.
<asp:FormView runat="server" id="fwHotelDetails" DataKeyNames="id" OnDataBound="fwHotelDetails_DataBound" OnModeChanging="fwHotelDetails_ModeChanging" >
<ItemTemplate>
// (..) some code here which outputs some data
<asp:Repeater runat="server" id="repScore">
<ItemTemplate>
<span class="item"> Some output here</span>
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
</ItemTemplate>
</asp:Repeater>
<EditItemTemplate>
Test test, anything??
</EditItemTemplate>
</ItemTemplate>
</asp:FormView>
I've tried thefollowing solutions in the code behind - none of them works:
protected void fwHotelDetails_ItemCommand(object sender, FormViewModeEventArgs e)
{
if (e.CommandName.Equals("Edit"))
{
fwHotelDetails.ChangeMode(e.NewMode);
}
}
and this:
protected void fwHotelDetails_ModeChanging(object sender, System.Web.UI.WebControls.DetailsViewModeEventArgs e)
{
fwHotelDetails.ChangeMode((FormViewMode)e.NewMode);
}
Clicking the Edit button only gives me the following error message:
The FormView 'fwHotelDetails' fired event ModeChanging which wasn't handled
What more needs to be done?
This page is a great reference for FormView controller: http://authors.aspalliance.com/aspxtreme/sys/web/ui/webcontrols/FormViewClass.aspx
Update: I've updated code to refelct Phaedrus suggestion.
Current status is that even after clicking Edit button, the content from ItemTemplate is loaded.
You have to specify which method handles the ModeChanging event. This event is raised when a FormView control attempts to switch between edit, insert, and read-only mode, but before the mode actually changes.
<asp:FormView OnModeChanging="fwHotelDetails_ModeChanging" />
The second parameter of your method signature is 'DetailsViewModeEventArgs' it should be 'FormViewModeEventArgs'.
void fwHotelDetails_ModeChanging(Object sender, FormViewModeEventArgs e)
{
}
Just Simply write code in formview's Item_Command
protected void formview_ItemCommand(object sender, FormViewCommandEventArgs e)
{
if (e.CommandName == "Edit")
{
formview.DefaultMode = FormViewMode.Edit;
formview.DataBind();
}
if (e.CommandName == "Cancel")
{
formview.DefaultMode = FormViewMode.ReadOnly;
formview.DataBind();
}
}