How to disable a particular CheckBoxList ListItem in ASP.NET? - asp.net

In my web page, there are two checkboxlists.
<asp:CheckBoxList ID="chk1" runat="server" AutoPostBack="True" onselectedindexchanged="chk1_SelectedIndexChanged" >
<asp:ListItem Value="1">1</asp:ListItem>
<asp:ListItem Value="2">2</asp:ListItem>
</asp:CheckBoxList>
and
<asp:CheckBoxList ID="ch2" runat="server" AutoPostBack="True" >
<asp:ListItem Value="3">3</asp:ListItem>
<asp:ListItem Value="4">4</asp:ListItem>
</asp:CheckBoxList>
If I checked 1 in chk1, 3 should be disabled in chk2, and if I checked 2 in chk1, 4 should be disabled in chk4.

Try this,
protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
{
if(chk1.selectedIndex==0)
{
chk2.Items[0].enabled=false;
}
else if(chk1.selectedIndex==1)
{
chk2.Items[1].enabled=false;
}
}

Please try with the below code snippet.
protected void chk1_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (ListItem item in chk1.Items)
{
switch (item.Value)
{
case "1":
if (chk2.Items.FindByValue("3") != null && item.Selected == true)
chk2.Items.FindByValue("3").Enabled = false;
else
chk2.Items.FindByValue("3").Enabled = true;
break;
case "2":
if (chk2.Items.FindByValue("4") != null && item.Selected == true)
chk2.Items.FindByValue("4").Enabled = false;
else
chk2.Items.FindByValue("4").Enabled = true;
break;
default:
break;
}
}
}

Related

how to enable/disable a textbox based on dropdown list value

I have 2 dropdown list values and for 1 of it when selected I want the one textbox out of 2 to get disabled.
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedValue=="Admin")
TextBox2.Enabled = false;
else
TextBox2.Enabled = true;
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem.Text.ToString()=="Admin")
TextBox2.Enabled = false;
else
TextBox2.Enabled = true;
}
Example:
<asp:DropDownList ID="DropDownList1" runat="server" CssClass="form-control">
<asp:ListItem Text="Admin" Value="1" />
<asp:ListItem Text="Other" Value="2" />
</asp:DropDownList>
If do you want to use Value, you can use "DropDownList1.SelectedValue.ToString()=="1""
If do you want to use Text, you can use "DropDownList1.SelectedItem.Text.ToString()=="Admin" "

How enable and disable button in DevExpress

I develop an application with DevExpress, but I have a problem in my code, I want a condition for my button "save", if condition "A" button save enable, but if condition "B" button save disable,but in my code the button "save" enable in the 2 cases.
how to fix this problem?
<% if (Condition == A )
{
%>
<td>
<dx:ASPxButton ID="BtSavelisteningScale" runat="server" HorizontalAlign="NotSet"
ImagePosition="Left" Text="Enregistrer" VerticalAlign="NotSet" ClientEnabled="true" ClientInstanceName="BtSavelisteningScale"
Wrap="Default" AutoPostBack="false" meta:resourcekey="BtSavelisteningScaleResource1">
<ClientSideEvents Click="function(s, e){if (ASPxClientEdit.ValidateGroup('grpVal')) {CPlEvalScale.PerformCallback('Enregistrer');} }" />
<%--<ClientSideEvents Click="function(s, e){ if (ASPxClientEdit.ValidateGroup('grpVal')) {GetScore();GvLs.PerformCallback('UpdateListenningScale');}}" />--%>
</dx:ASPxButton>
</td>
<% } %>
<% else if (Condition == B )
{
%>
<td>
<dx:ASPxButton ID="BtSavelisteningScaleFalse" runat="server" HorizontalAlign="NotSet"
ImagePosition="Left" Text="Enregistrer" VerticalAlign="NotSet" ClientEnabled="false" ClientInstanceName="BtSavelisteningScale"
Wrap="Default" AutoPostBack="false" meta:resourcekey="BtSavelisteningScaleResource1">
<ClientSideEvents Click="function(s, e){if (ASPxClientEdit.ValidateGroup('grpVal')) {CPlEvalScale.PerformCallback('Enregistrer');} }" />
<%--<ClientSideEvents Click="function(s, e){ if (ASPxClientEdit.ValidateGroup('grpVal')) {GetScore();GvLs.PerformCallback('UpdateListenningScale');}}" />--%>
</dx:ASPxButton>
</td>
<% } %>
By Default Set it Visiable = False Then
Page_Load Event or Page_Init Event Check your condition. example:
protected void Page_Load(object sender, EventArgs e)
{
if (Condition == A)
{
BtSavelisteningScale.Visiable = True;
BtSavelisteningScaleFalse.Visiable = False;
}
else if (Condition == B)
{
BtSavelisteningScale.Visiable = False;
BtSavelisteningScaleFalse.Visiable = True;
}
}
Protected void Page_init(object sender, EventArgs e)
{
if (Condition == A)
{
BtSavelisteningScale.Visiable = True;
BtSavelisteningScaleFalse.Visiable = False;
}
else if (Condition == B)
{
BtSavelisteningScale.Visiable = False;
BtSavelisteningScaleFalse.Visiable = True;
}
}

DropDownList's SelectedIndexChanged event calls all events

i'm using Asp .Net Web Forms. i have 2 dropDown list and 1 input field. I want when i change the item on the dropDown list or the field value to call some event but it calls all the events.
for example if i change size it calls Size_SelectedIndexChanged, then Color_SelectedIndexChanged, and then txtKolicina_TextChanged
if i change color it calls Color_SelectedIndexChanged, than Size_SelectedIndexChanged and then txtKolicina_TextChanged.
Any help?
<asp:DropDownList ID="Colors" runat="server" AutoPostBack="true" CssClass="form-control detal-page-input" Style="height: 30px;" OnSelectedIndexChanged="Colors_SelectedIndexChanged" AppendDataBoundItems="True" DataSourceID="LinqDataSource3" DataTextField="color" DataValueField="color" >
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="Size" runat="server" OnSelectedIndexChanged="Size_SelectedIndexChanged" AppendDataBoundItems="true" AutoPostBack="true" CssClass="form-control detal-page-input" Style="height: 30px;" DataSourceID="LinqDataSource2" DataTextField="size" DataValueField="size" EnableViewState="true">
<asp:ListItem Text="" Value=""></asp:ListItem>
</asp:DropDownList>
<asp:TextBox Name="txtKolicina" ID="txtKolicina" runat="server" CssClass="form-control form-numberone detal-page-input" OnTextChanged="txtKolicina_TextChanged" ></asp:TextBox>
this is backEnd
protected void Size_SelectedIndexChanged(object sender, EventArgs e)
{
//do something
}
protected void Colors_SelectedIndexChanged(object sender, EventArgs e)
{
//do something
}
protected void txtKolicina_TextChanged(object sender, EventArgs e)
{
//do something
}
UPDATE
public string[] GetColor()
{
CMS_Shop_ModuleDataContext db = new CMS_Shop_ModuleDataContext();
var color = (from p in db.CMS_Articles
where
p.articleID == int.Parse(HiddenFieldArticalId.Value) ||
p.sameAsArticleID == int.Parse(HiddenFieldArticalId.Value)
//where p.articleID == 10049 || p.sameAsArticleID == 10049
select p.color).Distinct();
return color.ToArray();
}
public int GetColorCount()
{
CMS_Shop_ModuleDataContext db = new CMS_Shop_ModuleDataContext();
var color = (from p in db.CMS_Articles
where (p.articleID == int.Parse(HiddenFieldArticalId.Value)
|| p.sameAsArticleID == int.Parse(HiddenFieldArticalId.Value))
&& p.color != ""
select p.color);
return color.Distinct().Count();
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (GetColorCount() == 0)
{
Colors.Visible = false;
lblBoja.Visible = false;
}
else
{
Colors.Visible = true;
lblBoja.Visible = true;
}
Looks like you use View State and repopulate your DropDownLists in code behind on Postback. Can you show OnInit and OnLoad ?
Or just try this code:
if (!IsPostBack) {
// populate all drop downs lists
}
Ok i found what was the problem.
a change
EnableViewState="false"
to
EnableViewState="true"

DropDownList selected item always first item in list

I have a DropDownList in a user control; however, no matter what I do I can't get any of the "selected" properties (SelectedItem, SelectedIndex, SelectedValue) to populate correctly. The value of all three of these properties is the first item in my list no matter which item was actually selected.
<asp:DropDownList ID="ParticipantsSelectList" runat="server">
<asp:ListItem Value="">Please select a team...</asp:ListItem>
<asp:ListItem value="{D37EFA0C-988A-4A2A-8D6E-80E3BAE00DEE}">Blue Team</asp:ListItem>
<asp:ListItem value="{7543E282-C9B8-435A-86A2-70E8E3BB38E5}">Green Team</asp:ListItem>
<asp:ListItem value="{F6BEF34A-215E-4179-9F4C-68F7C43D755F}">Orange Team</asp:ListItem>
<asp:ListItem value="{D50AD44A-686A-4BD3-B62E-D70ABF153AE5}">Red Team</asp:ListItem>
<asp:ListItem value="{DEFB7DA6-B0FF-4C36-A015-F3E8BC7AECA2}">Yellow Team</asp:ListItem>
<asp:ListItem value="{D37EFA0C-988A-4A2A-8D6E-80E3BAE00DEE},{7543E282-C9B8-435A-86A2-70E8E3BB38E5},{F6BEF34A-215E-4179-9F4C-68F7C43D755F},{D50AD44A-686A-4BD3-B62E-D70ABF153AE5},{DEFB7DA6-B0FF-4C36-A015-F3E8BC7AECA2}">All Teams</asp:ListItem>
</asp:DropDownList>
<asp:Button runat="server" Text="Submit" />
private void Page_Load(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(ParticipantsSelectList.SelectedValue)) // SelectedValue is always empty string (value of the first item in the list)
{
resultsPanel.Visible = true;
selectTeamLabel.Visible = false;
GenerateResultsTable();
}
else
{
resultsPanel.Visible = false;
selectTeamLabel.Visible = true;
}
}
Try using !Page.IsPostBack in Page_Load
private void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//Your Code....
}
}

ASP.NET get all values of radiobuttonlist that in repeater?

aspx file:
<asp:Repeater ID="Repeater_sorular" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="div_soru">
<div class="div_soru_wrapper">
<%#Eval("Subject")%>
<asp:RadioButtonList ID="RadioButtonList_secenekler" runat="server" Visible='<%# Eval("TypeId").ToString() == "1" %>'
DataSource='<%#Eval("Secenekler")%>' DataTextField='<%#Eval("OptionName")%>' DataValueField='<%#Eval("OptionId")%>'>
</asp:RadioButtonList>
<asp:CheckBoxList ID="CheckBoxList_secenekler" runat="server" Visible='<%# Eval("TypeId").ToString() == "2" %>'
DataSource='<%#Eval("Secenekler")%>' DataTextField='<%#Eval("OptionName")%>' DataValueField='<%#Eval("OptionId")%>'>
</asp:CheckBoxList>
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
and codebehind:
SpAnketDataContext db = new SpAnketDataContext();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindRepeaterSorular();
}
}
protected void ImageButton_kaydet_OnCommand(object source, CommandEventArgs e)
{
}
private void BindRepeaterSorular()
{
int anket_id = 3;
var sorular = from soru in db.TableSurveyQuestions
where soru.SurveyId == anket_id
select new
{
soru.TypeId,
soru.Subject,
soru.QuestionId,
soru.SurveyId,
soru.QueueNo,
SurveyTitle = soru.TableSurvey.Title,
TypeName = soru.TableSurveyQuestionType.TypeName,
Secenekler = from secenekler in soru.TableSurveyOptions
select new
{
secenekler.OptionId,
secenekler.OptionName,
secenekler.QuestionId,
}
};
Repeater_sorular.DataSource = sorular;
Repeater_sorular.DataBind();
}
this is the code that I try to get values but I can get only last radiobuttonlist values.
protected void Repeater_sorular_ItemCommand(object sender, RepeaterCommandEventArgs e)
{
foreach (RepeaterItem item in Repeater_sorular.Items)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
CheckBoxList CheckBoxList1 = (CheckBoxList)e.Item.FindControl("CheckBoxList_secenekler");
RadioButtonList RadioButtonList1 = (RadioButtonList)e.Item.FindControl("RadioButtonList_secenekler");
if (CheckBoxList1 != null)
{
foreach (ListItem li in CheckBoxList1.Items)
{
TableSurveyVote votes=new TableSurveyVote();
votes.MemberId=1;
votes.OptionId=Int32.Parse(li.Value);
db.TableSurveyVotes.InsertOnSubmit(votes);
db.SubmitChanges();
}
}
if (RadioButtonList1 != null)
{
foreach (ListItem li in RadioButtonList1.Items)
{
TableSurveyVote votes=new TableSurveyVote();
votes.MemberId=1;
votes.OptionId=Int32.Parse(li.Value);
db.TableSurveyVotes.InsertOnSubmit(votes);
db.SubmitChanges();
}
}
}
}
}
What is wrong?
You get the same results as you say on the comment because you do not use the populate from the foreach loop (the item) but use the same reference e.Item on the loop.

Resources