Show confirmation box then edit textbox field based on answer - c#

I've got an asp.net webforms page which has a drop down filled with Categories. If the user selects the Category "ER" or "DR", a Description textbox will be populated based on which one is chosen. However, I need to inform the user that their text in the Description field will be lost if they switch categories.
submit_ticket.aspx
function ShowConfirmation(ddlCategory) {
//Not sure what goes here.
}
<asp:DropDownList ID="ddlCategory" runat="server" CssClass="DropDownList" OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" AutoPostBack="true" />
Submit_ticket.aspx.cs
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
if ((ddlCategory.SelectedItem.Text == "DR") || ddlCategory.SelectedItem.Text == "ER")
ddlCategory.Attributes.Add("onChange", "return ShowConfirmation(this);");
if (ddlCategory.SelectedItem.Text == "DR")
txtDescription.Text = "DR Template";
else if (ddlCategory.SelectedItem.Text == "ER")
txtDescription.Text = "ER Template";
}
}
There are 7 Categories in total but I only need this functionality on the two listed above. The "template" used for those Categories is long and it can't be an option to populate the Description textbox with Javascript.
Any help is greatly appreciated.

Remove the AutoPostBack from the dropdown and add your own js function to the onchange event.
<asp:DropDownList ID="DropDownList1" runat="server" onchange="func();">
<asp:ListItem Text="01"></asp:ListItem>
<asp:ListItem Text="02"></asp:ListItem>
<asp:ListItem Text="03"></asp:ListItem>
</asp:DropDownList>
In your js function get a response from the user. If 'yes', force a postback using the __doPostBack function built-in to asp.net. Include the name of the control and 'true' as parameters.
function func() {
if (confirm("change?")) {
// parameters are: '__EVENTTARGET' and '__EVENTARGUMENT'.
__doPostBack("DropDownList1", "true");
}
}
Check the __EVENTARGUMENT when the page loads.
protected void Page_Load(object sender, EventArgs e)
{
if(Request.Params.Get("__EVENTARGUMENT") == "true")
{
// call a method.
TextChanger();
}
}
protected void TextChanger()
{
if (DropDownList1.SelectedItem.Text == "02")
{
Label2.Text = "changed text";
}
}

Related

using edit link button in gridview hides select in dropdown

In my application i have gridview with edit button. By clicking edit button in gridview the listitem of DropDownList gets replaced by the text that is in gridview these are my dropdown list values
--select--
Roller
Heater
Aspx Code:
<asp:DropDownList ID="ddlvalue" runat="server" Width="175px" AppendDataBoundItems="true">
<asp:ListItem>Select</asp:ListItem>
<asp:ListItem>Roller</asp:ListItem>
<asp:ListItem>Heater</asp:ListItem>
</asp:DropDownList>
CS Code:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
ddlValue.SelectedItem.Text = (GridView1.SelectedRow.FindControl("lblValue") as Label).Text;
}
here is what you should do. you care currently changing the text of selected item instead of finding the correct item and selecting it. here is how you should do it
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string value=(GridView1.SelectedRow.FindControl("lblValue") as Label).Text;
try
{
foreach(ListItem li in ddlValue.Items)
{
if(li.Text==value)
{
li.Selected=true;
}
else
{
li.Selected=false;
}
}
}
catch
{
}
}
string value = (GridView1.SelectedRow.FindControl("lblRole") as Label).Text;
try
{
ddlvalue.ClearSelection();
ddlvalue.Items.FindByText(value).Selected = true;
}
catch
{
}

Disable buttons through Radio button

I have 2 Radio buttons in my requirement.
Single Field Scrambler and Multi field Scrambler.
User must select the “Single field scrambler” or “Multi field scrambler” radio button based on the scrambling requirement.
If user selects “Single field scrambler”, the below buttons should be disabled and they must select a field required for scrambling.
Select Config File
View Config File
Modify Config File
This is my Radio Button List Code
<asp:RadioButtonList ID="RadioButtonList1" runat="server" RepeatDirection="Horizontal"OnSelectedIndexChanged="RadioButtonList1_SelectedIndexChanged">
<asp:ListItem Text="Single Field Scrambler" Value="singlefieldscramble"> </asp:ListItem>
<asp:ListItem Text="Multi Field Scrambler" Value="multifieldscramble"></asp:ListItem> </asp:RadioButtonList>
How can we disable buttons when user clicks "Single Field Scrambler".
Any Help Please.
You can call SelectedIndexChanged event for the radio buttons like this,
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
if(RadioButtonList1.SelectedItem.ToString()=="Single field scrambler" || RadioButtonList1.SelectedItem.ToString()=="singlefieldscrambler")
{
btnvcf.Enabled = false;
btnmcf.Enabled = false;
DropDownConfigFile.Enabled = false;
}
}
similar code for the radio button list is here:
http://asp-net-example.blogspot.in/2009/03/how-to-use-onselectedindexchanged-event_18.html
You can call SelectedIndexChanged event for the radio buttons like this:
protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgse){
for (int i = 0; i < RadioButtonList1.Items.Count; i++)
{
if (RadioButtonList1.Items[i].Selected == true)
{
RadioButtonList2.Items[i].Selected = false;
RadioButtonList2.Items[i].Enabled = false;
}
if (RadioButtonList1.Items[i].Selected != true)
{
RadioButtonList2.Items[i].Enabled = true;
}
}
}

how to get checkbox list unchecked item

i have a datalist contains checkboxlist.
<asp:DataList ID="dtlstfilter" runat="server">
<ItemTemplate>
<asp:CheckBoxList ForeColor="Gray" AutoPostBack="true" OnSelectedIndexChanged="chklist_SelectedIndexChanged" ID="chklist"
runat="server">
</asp:CheckBoxList>
</ItemTemplate>
</asp:DataList>
when i check one from the checkbox list in the SelectedIndexChanged event i got the selected value using
CheckBoxList c = (CheckBoxList)sender;
string selectedvalue= c.SelectedValue;
likewise how can get the value from a checkboxlist if i uncheck one from the checkboxlist
The SelectedIndexChanged gets also fired if you uncheck a CheckBox. So it works the same way. But if you want to know the (now) unchecked item(s), you have to store the old selection somewhere, for example in the ViewState:
private IEnumerable<string> SelectedValues
{
get
{
if (ViewState["SelectedValues"] == null && dtlstfilter.SelectedIndex >= -1)
{
ViewState["SelectedValues"] = dtlstfilter.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.Value)
.ToList();
}else
ViewState["SelectedValues"] = Enumerable.Empty<string>();
return (IEnumerable<string>)ViewState["SelectedValues"];
}
set { ViewState["SelectedValues"] = value; }
}
protected void chklist_SelectedIndexChanged(Object sender, EventArgs e)
{
CheckBoxList c = (CheckBoxList)sender;
var oldSelection = this.SelectedValues;
var newSelection = c.Items.Cast<ListItem>()
.Where(li => li.Selected)
.Select(li => li.Value);
var uncheckedItems = newSelection.Except(oldSelection);
}
This should even work if multiple checkboxes can be selected.
You can take the jQuery Route if it suits you...
if (!IsPostBack)
{
foreach (ListItem item in chkList.Items)
{
//adding a dummy class to use at client side.
item.Attributes.Add("class", "chkItem");
}
}
Put one button on your form with style display : none. And a Hidden Field to track the currently checked checkbox.
<asp:Button ID="hdnButton" runat="server" style="display:none;" OnClick="hdnButton_Click"/>
<asp:HiddenField ID="hdnCurrent" runat="server" />
The jQuery Part....
$(".chkItem input:checkbox").change(function(){
$("#hdnCurrent").val($(this).attr("id") + "|" + $(this).attr("checked"));
$("#hdnButton").click();
});
You can use more hidden fields if you don't want to do string operations on backend. Depends on your taste.
Then handle the button click event like below.
protected void hdnButton_Click(object sender, EventArgs e)
{
String[] Value = hdnCurrent.Value.Split('|');
if (Value[1] == "true")
{
//Do operations here when the check box is checked
}
else
{
//Do operations here when the check box is unchecked
}
//Value[0] contains the id of the check box that is checked/unchecked.
}

asp.net c# is checkbox checked?

How do I determine if the checkbox is checked or not checked?
Very perplexed why this is not working - it is so simple!
On my web form:
<asp:CheckBox ID="DraftCheckBox" runat="server" Text="Save as Draft?" />
<asp:Button ID="PublishButton" runat="server" Text="Save" CssClass="publish" />
Code behind which runs in the click event for my save button:
void PublishButton_Click(object sender, EventArgs e)
{
if (DraftCheckBox.Checked)
{
newsItem.IsDraft = 1;
}
}
When debugging it never steps into the If statement when I have the checkbox checked in the browser. Ideas?!
I think there maybe some other code affecting this as follows...
In Page_load I have the following:
PublishButton.Click += new EventHandler(PublishButton_Click);
if (newsItem.IsDraft == 1)
{
DraftCheckBox.Checked = true;
}
else
{
DraftCheckBox.Checked = false;
}
newsItem is my data object and I need to set the checkbox checked status accordingly.
When the save button is hit I need to update the IsDraft property based on the checked status of the checkbox:
void PublishButton_Click(object sender, EventArgs e)
{
if (IsValid)
{
newsItem.Title = TitleTextBox.Text.Trim();
newsItem.Content = ContentTextBox.Text.Trim();
if (DraftCheckBox.Checked)
{
newsItem.IsDraft = 1;
}
else
{
newsItem.IsDraft = 0;
}
dataContext.SubmitChanges();
}
}
So, isDraft = 1 should equal checkbox checked, otherwise checkbox should be un-checked. Currently, it is not showing this.
Specify event for Button Click
<asp:Button ID="PublishButton" runat="server" Text="Save" onclick="PublishButton_Click" />
What i can see you have not got a OnClick on your button. So like this:
<asp:CheckBox ID="DraftCheckBox" runat="server" Text="Save as Draft?" />
<asp:Button ID="PublishButton" runat="server" OnClick="PublishButton_Click"
Text="Save" CssClass="publish" />
And then the function should work like it is:
protected void PublishButton_Click(object sender, EventArgs e)
{
if (DraftCheckBox.Checked)
{
newsItem.IsDraft = 1;
}
}
Please replace code as following code..
void PublishButton_Click(object sender, EventArgs e)
{
if (DraftCheckBox.Checked==True)
{
newsItem.IsDraft = 1;
}
}
Try adding onclick="PublishButton_Click" in the button field on the form. And I don't know if it makes a difference, but generated event handlers are protected void.
For me the best solution in the end has been to create 2 separate pages: 1 for editing a news articles & 1 for a new news article. So Ill never then be in the position of a new news data object being created when the page reloads.
Both page return to the article index list page when the save button is pressed and that seems to work with being able to save the state of the draft checkbox and then show the state on the edit page.
The checkbox.checked isn't used in the context you want it to (this is a boolean that if true, will make the checkbox look checked).
What you could do is to use instead a checkboxlist. Then you could do the following:
foreach(Listitem li in CheckBoxList1.Items)
{
if (li.Selected)
{
NewsItem.Isdraft = 1;
}
}

Not able to display content in <EditItemTemplate> in FormView

What am I doing wrong since the content in the < EditItemTemplate > is not displayed when I click the Edit button?
<asp:FormView runat="server" id="fwHotelDetails" DataKeyNames="id" OnDataBound="fwHotelDetails_DataBound" OnModeChanging="fwHotelDetails_ModeChanging">
<ItemTemplate>
//display content here
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit" />
</ItemTemplate>
<EditItemTemplate>
This text should be displayed when I click the Edit button
<asp:LinkButton runat="server" ID="UpDateButton" CausesValidation="false" CommandName="Update" Text="Lagre" />
</EditItemTemplate>
</asp:FormView>
Update
This is my code-behind:
namespace development.templates
{
public partial class HotelDetails : TemplatePage
{
static Hotel hotel;
protected DataRow drHotel;
DataTable dtCriteria;
DataTable dtHotel;
private HotelCriteria hotelCriteria;
protected void Page_Load(object sender, EventArgs e)
{
int hotelID = Convert.ToInt32(Request.QueryString["hotelid"].ToString());
if (!IsPostBack)
{
if (hotelID != 0)
{
// Create Hotel instance based on hoteID.
hotel = new Hotel(hotelID);
drHotel = hotel.hotelData.Rows[0];
dtHotel = hotel.getHotelsByCity(drHotel["city"].ToString());
// Hotel scrore is calculated from a score which is derived from certain criterias.
hotelCriteria = new HotelCriteria(hotelID);
dtCriteria = hotelCriteria.getHotelCriteria();
//Set datasource for hotel list in right sidebar.
hotelListByCity.DataSource = correctList(dtHotel, hotelID);
hotelListByCity.DataBind();
// Set datasource for current hotel
fwHotelDetails.DataSource = hotel.hotelData;
fwHotelDetails.DataBind();
}
}
}
protected void fwHotelDetails_DataBound(object sender, EventArgs e)
{
//Find the criteria list and set the datasource
Repeater rep = (Repeater)fwHotelDetails.FindControl("repCriteriaScore");
rep.DataSource = this.dtCriteria;
rep.DataBind();
// Controll is user is logged in. If logged in, then user may add, edit or delete hotel record.
System.Security.Principal.IPrincipal user = Context.User;
if ((user != null) && user.Identity.IsAuthenticated){
Panel panel = (Panel)fwHotelDetails.FindControl("administrationPanel");
panel.Visible = true;
}
}
protected void fwHotelDetails_ModeChanging(object sender, FormViewModeEventArgs e)
{
switch (e.NewMode)
{
case FormViewMode.Edit:
MessageLabel.Text = "Edit mode";
fwHotelDetails.ChangeMode(FormViewMode.Edit);
break;
case FormViewMode.ReadOnly:
MessageLabel.Text = "Read mode";
break;
case FormViewMode.Insert:
MessageLabel.Text = "Insert mode";
break;
}
}
}
}
The EditItemTemplate also won't show up if the record is empty. In other words, if you have a gridview that you select a detail record from that is feeding the formview, and their is no detail for the selected grid item, then the form won't show up at all. I check to see if the detail record is null, and if so, I set the formview or detailsview to "insert" mode. so they can enter a new record.
In your function fwHotelDetails_ModeChanging, add this:
fwHotelDetails.ChangeMode(FormViewMode.Edit)
i.e.
Protected Sub fwHotelDetails_ModeChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewModeEventArgs) Handles fwHotelDetails.ModeChanging
fwHotelDetails.ChangeMode(FormViewMode.Edit)
End Sub
Ok have you tried putting a breakpoint on fwHotelDetails_ModeChanging and then debugging the App? Does the breakpoint get hit when you click the edit button.
At least this will tell you where your problem lies. That is [1] the events are not hooked up correctly or [2] there is something going wrong with ChangeMode.
I realise this isnt a solution but if you tell me whether the breakpoint hits i can help you further.

Categories