Why my CheckBox.Checked Property will reset? - c#

Using: .NET 3.5SP1, VS2008
I was editting someone else asp.net script, he did the Data retreving at the Page_Load while Page is not postback.
I could see the data was populated into the DropDownList properly even after I refresh, navigates, postback in the page.
I added couples more DropDownList and some CheckBoxes into the script, only the DropDownList I added got populated properly. But not the CheckBox.
So I do a test in a new project, which is similar to its script structure:
ASPX:
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
%>
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
}
%>
</div>
</form>
Code-Behind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.CheckBox1.Checked = true;
this.CheckBox1.Text = "Hello CheckBox";
this.TextBox1.Text = "Hello TextBox";
this.DropDownList2.SelectedValue = "Item2";
}
}
}
So as you see the code, when the page first load, the CheckBox1's text will change, Checked will be true, so as other TextBox and DropDownList2
After I select DropDownList1's item to Item2, when the CheckBox1, TextBox1, DropDownList2 nothing got setted, except the CheckBox1.Text.
Why is this happen?
EDIT:
I tried to put them into Panel, in this way it work. But the problem is the program I am editting is using the format above.. So I am not allow to change them all to Panel.
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
<%
if (DropDownList1.SelectedValue == "Item2")
{
this.MyPanel.Visible = true;
}
else
{
this.MyPanel.Visible = false;
}
%>
<asp:Panel ID="MyPanel" runat="server" Visible="false" >
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1" />
<asp:TextBox ID="TextBox1" runat="server" Text="" />
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true">
<asp:ListItem>Item1</asp:ListItem>
<asp:ListItem>Item2</asp:ListItem>
</asp:DropDownList>
</asp:Panel>
</div>
</form>

Try out setting EnableViewState and ViewStateMode, perhaps some of the parent controls has disabled it so default Inherit value has been applied.
MSDN:
The default value of the ViewStateMode property for a Web server
control in a page is Inherit. As a result, if you do not set this
property at either the page or the control level, the value of the
EnableViewState property determines view-state behavior.
<asp:CheckBox ID="CheckBox1" runat="server" Text="CheckBox 1"
EnableViewState="true"
ViewStateMode="Enabled" />

Related

How to show or hide the text box depends on the drop down selection

When the selection of the dropdown is changed by the user, the corresponding textbox will show depends on user's selection. Let's say when user select "A" from the dropdown, the Textbox "A" will shown, while the other textbox will be invisible.
The issue is when user select "A" in the drop down, the other two text box won't disappear.
aspx
<asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="A" Text="A" />
<asp:ListItem Value="B" Text="B" />
<asp:ListItem Value="C" Text="C" />
</asp:DropDownList>
A <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
B <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
C <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
aspx.cs
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "A")
{
TextBox2.Visible = false;
TextBox3.Visible = false;
}
}
Make all textbox properties set to visible false and then try in dropdown selection chagned event.
<asp:TextBox ID="TextBox1" runat="server" visible="false"></asp:TextBox>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text == "A")
{
TextBox2.Visible = false;
}
else
{
TextBox2.Visible = true;
}
}
It's not working because you should refresh your page. You should add AutoPostBack="true" property to your DropDownList.
Also, you can use UpdatePanel to don't need to refresh all your page.
In aspx use this:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="A" Text="A" />
<asp:ListItem Value="B" Text="B" />
<asp:ListItem Value="C" Text="C" />
</asp:DropDownList>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
I hope it helps you
If your goal is just to toggle visibility of the textboxes based on drop down selection, you should write a JavaScript function on your page which will take index as parameter. Then it will change the visibility of the indexed text box to true and false for the other text boxes.
Something like this:
Dropdown event:
on-click=“return toggleText(selectedIndex);”
<script>
function toggleText(index) {
// show or hide textbox by setting it’s style display:block or display:none respectively
return false; // ensures the page is not posted-back to server
}
</script>

How to to get hiddenfield value in ItemTemplate

Just want to ask how can I find hiddenfield in repeater because my problem I have button and I want to get the associate hiddenfield inside ItemTemplate because I get null value when I try to get hiddenfield value
<asp:Repeater ID="rp_resList" runat="server" OnItemDataBound="rp_resList_ItemDataBound">
<ItemTemplate>
<div class="resourcesResult">
<asp:HiddenField ID="hf_resID" runat="server" Value='<%# Eval("Id") %>' />
<a href='<%# Eval("pageID") %>'><%# Eval("name") %></a>
<br />
<asp:Literal ID="litSummary" runat="server" Text='<%# Eval("summary") %>'></asp:Literal>
<br />
<%-- <asp:Repeater ID="rp_tagsSkill" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>--%>
<asp:Repeater ID="rp_tagsTopics" runat="server">
<ItemTemplate>
<h6>
<%# Eval("Description") %>
</h6>
</ItemTemplate>
</asp:Repeater>
<div id="controls">
<asp:ImageButton ID="imgbtnBookmark" runat="server" OnClick="imgbtnBookmark_Click" />
<asp:DropDownList ID="ddlGroup" runat="server" DataSourceID="SqlDS_Groups" DataTextField="name" DataValueField="id" AppendDataBoundItems="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" >
<asp:ListItem Value="-1">Select Group</asp:ListItem>
protected void imgbtnBookmark_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Bookmark bm = new Bookmark();
HiddenField hiddenField = rptGroup.FindControl("hf_resID") as HiddenField;
bm.UserID =
Guid.Parse(Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString());
bm.Resoursce.ResourceID = Convert.ToInt32(hiddenField.Value);
Bookmark.Insert(bm);
}
Try this, change you button tag to pass id as CommandArgument value
<asp:ImageButton ID="imgbtnBookmark" runat="server"
OnClick="imgbtnBookmark_Click"
CommandArgument='<%# Eval("Id") %>'/>
in your button click event you can access id using
bm.Resoursce.ResourceID = Convert.ToInt32(e.CommandArgument.ToString());
Change rptGroup.FindControl("hf_resID") as HiddenField; to
e.Item.FindControl("hf_resID")....
I hope this link will help you .
jquery
function showid(dllval) {
var ID = $(dllval).parent().parent().find('[id*="hiddenID"]').val();
alert(ID)
}
asp.net
<asp:DropDownList ID="ddl" runat="server" onclick="showid(this);" >
</asp:DropDownList>
<asp:HiddenField ID="hiddenID" runat="server" Value='<%#Eval("ID")%>' />

Assign value to dropdown list via query string

I stored the selected value in a DDL list on Page 1 in query string variable and then tried to assign it on page 2, to the same drop down list coming from user control page. But while assigning the value to the DDList on page 2 I am getting either an array index out of bound exception or a null value exception.
I have debugged and verified that the query string is correct, but it is unable to assign this value to the ddl list.Code pasted below:
<telerik:RadComboBox
ID="cmbSearchOaO"
runat="server"
AutoPostBack="true"
AppendDataBoundItems="true"
Width="200px"
DataSourceID="odsOwnedAndOperated"
DataTextField="Owned_And_Operated_Nm"
DataValueField="Owned_And_Operated_Id"
OnSelectedIndexChanged="PopulateApplicationTypeDropDown">
</telerik:RadComboBox>
ddl2.SelectedValue = Request.QueryString["No2"];
ddl2.FindItemByValue(Request.QueryString["No2"].ToString()).Selected = true;
The correct value is populated in Request.QueryString["No2"] , but I need to store it on LHS i.e on ddl list.
Try this solution. In my example I used ASP.NET DropdownList control
User control that holds the dropdownlist to be used by page1 and page 2
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="DDlUserControl.ascx.cs" Inherits="WebApplication2.DDlUserControl" %>
<asp:DropDownList ID="ddlTest" runat="server" AutoPostBack="True" Height="20px"
Width="223px">
<asp:ListItem Value="1">Item 1</asp:ListItem>
<asp:ListItem Value="2">Item 2</asp:ListItem>
<asp:ListItem Value="3">Item 3</asp:ListItem>
<asp:ListItem Value="4">Item 4</asp:ListItem>
<asp:ListItem Value="5">Item 5</asp:ListItem>
<asp:ListItem Value="6">Item 6</asp:ListItem>
<asp:ListItem Value="7">Item 7</asp:ListItem>
<asp:ListItem Value="8">Item 8</asp:ListItem>
</asp:DropDownList>
Page1 html page
<p>
<uc1:DDlUserControl ID="DDlUserControl1" runat="server" />
</p>
<p>
<asp:Button ID="btnSubmit" runat="server" onclick="btnSubmit_Click"
Text="Go to Page 2" />
</p>
Page1 code behind
protected void btnSubmit_Click(object sender, EventArgs e)
{
var ddl = DDlUserControl1.FindControl("ddlTest") as DropDownList;
Response.Redirect("Page2.aspx?no="+ddl.SelectedValue);
}
Page2 html
<div>
This is page 2<br />
<br />
<uc1:DDlUserControl ID="DDlUserControl1" runat="server" />
</div>
Page2 code behind
protected void Page_Load(object sender, EventArgs e)
{
var selectedVal = Request.QueryString["no"];
var ddl = DDlUserControl1.FindControl("ddlTest") as DropDownList;
ddl.SelectedValue = selectedVal;
}

TextBox inside Accordion inside Datalist... findcontrol not working

I've searched, but no luck...
I have a textbox inside an accordion control, that's inside a datalist... I want to allow the accordion form to submit some values, but I can't get those values out of the textbox, and findcontrol isn't working.
<asp:DataList ID="AddProjectDataList" runat="server">
<ItemTemplate>
<asp:HiddenField ID="clientid" runat="server" Value='<%# Eval("mmmclientlistid") %>'></asp:HiddenField>
<asp:Table ID="ProjectTableClass" runat="server" style="width:600px;height:600px"><asp:TableRow><asp:TableCell VerticalAlign="Top">
<b>New <asp:Label ID="ProjectTypeLabel" Text='<%# Eval("ProjectTypeName") %>' runat="server"></asp:Label> Project</b>
<table class="AddProject" cellpadding="5">
<tr>
<td valign="top">
<b>Campaign</b> information:
</td>
<td>
<asp:DropDownList ID="DDCampaignList" runat="server" AutoPostBack="true" OnSelectedIndexChanged="OnChange_selectCampaign" AppendDataBoundItems="True" >
<asp:ListItem Text="SELECT A CAMPAIGN:" Value="-1"></asp:ListItem>
</asp:DropDownList>
<br />
<ajaxToolkit:Accordion
ID="CampaignAccordion"
runat="Server"
SelectedIndex="1"
HeaderCssClass="accordionHeader"
HeaderSelectedCssClass="accordionHeaderSelected"
ContentCssClass="accordionContent"
AutoSize="None"
FadeTransitions="true"
TransitionDuration="250"
FramesPerSecond="40"
RequireOpenedPane="false"
SuppressHeaderPostbacks="true">
<Panes>
<ajaxToolkit:AccordionPane ID="AccordionPane1" runat="server"
HeaderCssClass="accordionHeader"
HeaderSelectedCssClass="accordionHeaderSelected"
ContentCssClass="accordionContent">
<Header>
<asp:LinkButton ID="AddCampaignLink" runat="server">Or Click to Add New Campaign</asp:LinkButton> </Header>
<Content>
<asp:TextBox ID="campaignNameTextBox" Style="width: 400px" runat="server"></asp:TextBox><br />
Description (optional):<br />
<asp:TextBox ID="campaignDescriptionTextBox" runat="server" TextMode="MultiLine"
Columns="30" Rows="3"></asp:TextBox>
<br />
<asp:Button ID="AddCampaignButton" runat="server" Text="Add New Campaign" OnClick="AddCampaign_Click" />
</Content>
</ajaxToolkit:AccordionPane>
</Panes>
<HeaderTemplate>...</HeaderTemplate>
<ContentTemplate>...</ContentTemplate>
And then in the codebehind
protected void AddCampaign_Click(object sender, EventArgs e)
{
//click to add campaign
//campaignname
//clientid
HiddenField EID = (HiddenField)FindControl("HiddenFieldEID");
TextBox campaignNameTextBox = (TextBox)AddProjectDataList.Items[0].FindControl("campaignNameTextBox");
TextBox campaignDescriptionTextBox = (TextBox)AddProjectDataList.Items[0].FindControl("campaignDescriptionTextBox");
tbl_campaign newcampaign = new tbl_campaign();
newcampaign.clientID = Convert.ToInt32(DDClientList.SelectedValue);
newcampaign.employeeID = Convert.ToInt32(HiddenFieldEID.Value);
newcampaign.campaignName = campaignNameTextBox.Text;
newcampaign.campaignDescription = campaignDescriptionTextBox.Text;
db.AddTotbl_campaign(newcampaign);
db.SaveChanges();
}
If I get rid of the accordion pane, it works fine. The control is located no problem. But with the accordion, no such luck. (The "hiddenFieldEID" field is located just fine because it's outside of the accordion.)
Do I need a separate onItemDataBound event in the DataList control? If so, what goes in there, and how do I relate it to the buttonclick event?
Thanks!
Well, I might have just figured it out.
I did this:
Control CampaignAccordion = (Control)AddProjectDataList.Items[0].FindControl("CampaignAccordion");
TextBox campaignNameTextBox = (TextBox)CampaignAccordion.FindControl("campaignNameTextBox");
TextBox campaignDescriptionTextBox = (TextBox)CampaignAccordion.FindControl("campaignDescriptionTextBox");
And it worked. No separate databinding event. Is this the normal way to do it?

Asp.net buttons inside control inside gridview not working

I need to display a list of clients, but display them differently based on a parameter.
To do this, I have a gridvew, and inside there is a user control. That control has an "if" based on the type.
My problems:
If I add a button inside the control, when it is pressed I get a button validation error.
If I disable validation errors (enableEventValidation="false"), I get button commands to work, but I'm not able to change values on the control either with full postbacks or an updatepanel.
<asp:GridView ID="gvClients" runat="server" AutoGenerateColumns="False" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<xxx:ClientListGridItem ID="ClientListItem1" runat="server" Client='<%# ((Client) Container.DataItem) %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
ClientListGridItem.ascx :
<% if (Client.Style >= 100)
{
%>
<div class="ClientListItem1">
...
<%
}
else
{
%>
<div class="ClientListItem2">
<asp:Button ID="Button2" runat="server" onclick="Button1_Click" Text="Button" />
...
<%
}
%>
I'm sure there is prettier, more object oriented way to do this too...
Changing ClientListGridItem.ascx into:
<asp:Panel id="Div1" CssClass="ClientListItem1" runat="server">
...
</asp:Panel>
<asp:Panel id="Div2" CssClass="ClientListItem2" runat="server">
<asp:Button ID="Button2" runat="server" onclick="Button1_Click" CausesValidation="false" Text="Button" />
..
</asp:Panel>
<script runat="server">
override void OnDataBinding(EventArgs e) {
Div1.Visible = Client.Style >= 100;
Div2.Visible = ! Div1.Visible;
}
</script>
should work.

Categories

Resources