RadComboBox pre selection - c#

First of all what I want to do is to pre select a value in my RadComboBox ,and if this value is not selected something else is selected then change the visibility to of some specific fields hidden.
My problem is that I'm able to make my pre select but somehow I can not change the status of my visibility for my specific fields when this pre selected value has changed.
What I have tired is to do it with a standard event OnSelectedIndexChanged but some how this is not triggering why so ever.. I have also added AutoPostBack=true as well as ViewStateMode=Enabled"
First my field's
Here comes my preslect as well here I would like to trigger the visibility change
<div class="formRowDiv">
<asp:Label ID="Activitylbl" runat="server" Text="Activity" CssClass="formLabel" />
<telerik:RadComboBox ID="rcbActivity" CssClass="rowForm" ViewStateMode="Enabled" runat="server" Width="260px" EmptyMessage="- Activity -"
DataTextField="ActivityId" DataValueField="ActivityId" AutoPostBack="true" OnSelectedIndexChanged="rcbActivity_SelectedIndexChanged">
</telerik:RadComboBox>
<asp:RequiredFieldValidator runat="server" Display="Dynamic" ControlToValidate="rcbActivity"
ErrorMessage="Can not be empty" CssClass="rowFormValidation" />
</div>
What I want to hide:
<div class="formRowDiv">
<asp:Label ID="ActivityDescription" runat="server" Text="ActivityDescription" CssClass="formLabel" Visible="false"/>
<telerik:RadTextBox runat="server" ID="rtbActivityDescription" Wrap="true" Height="50" TextMode="MultiLine" AutoPostBack="true" CssClass="rowForm" ReadOnly="true" Visible="false" />
</div>
How I do my pre selection :
In my databind method that is called in my Page_Load
I firrst loop and then do a pre select
foreach (Activity item in ctx.Activity.OrderBy(l =>l.Code))
{
rcbActivity.Items.Add(new RadComboBoxItem(item.FullActivity, item.ActivityId.ToString()));
if (rcbActivity.Items.FindItemByValue("4") != null)
{
rcbActivity.SelectedIndex = rcbActivity.Items.IndexOf(rcbActivity.Items.FindItemByValue("4"));
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}
}
Here is how I would hide my Fields
protected void rcbActivity_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
ActivityDescription.Visible = true;
rtbActivityDescription.Visible = true;
rtbActivityDescription.ReadOnly = false;
}

In case your controls are in an update panel then try removing it if the update panel is not so important and see if the changes u make to the controls in the server side are getting affected properly

Related

Bind Gridview on Treeview Node Click

I want to bind gridview on treenode click.code works fine without error but in UI nothing changes, but when I use same code on button click, Gridview binds data properly.
My apsx code is
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div style="overflow: scroll; height: 450px;">
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:TreeView ID="Folder_Treeview" runat="server" ShowLines="true" LeafNodeStyle-CssClass="childnode"
Style="" ForeColor="Blue" SelectedNodeStyle-ForeColor="Green" OnSelectedNodeChanged="Folder_Treeview_SelectedItemChanged">
</asp:TreeView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<asp:GridView ID="GridView1" CssClass="grid" GridLines="None" ShowFooter="true" AllowPaging="true"
PageSize="5" AutoGenerateColumns="false" OnPageIndexChanging="GridView1_PageIndexChanging"
runat="server">
<Columns>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" runat="server" Text='<%#Eval("Name")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Length">
<ItemTemplate>
<asp:Label ID="lblLen" runat="server" Text='<%#Eval("Length")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="File Extention">
<ItemTemplate>
<asp:Label ID="lblFileType" runat="server" Text='<%#Eval("Extension")%>'>
</asp:Label></ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Creation Date & Time">
<ItemTemplate>
<asp:Label ID="lblDateTime" runat="server" Text='<%#Eval("CreationTime")%>'>
</asp:Label></ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<p>
<asp:Label Text="" ID="lblMsg" runat="server"></asp:Label></p>
CS Code:
protected void Folder_Treeview_SelectedItemChanged(object sender, EventArgs e)
{
TreeNode node = this.Folder_Treeview.SelectedNode;
SetFolderPath(node);
}
public void SetFolderPath(TreeNode node)
{
Session["ParentFolderId"] = node;
// System.IO.DirectoryInfo RootDir = new System.IO.DirectoryInfo(Server.MapPath("~/"));
string CurrNode = node.Text;
string separator = "\\";
Folder_Treeview.PathSeparator = Convert.ToChar(separator);
while (node.Parent != null)
{
CurrNode = node.Parent.Text + this.Folder_Treeview.PathSeparator + CurrNode;
node = node.Parent;
}
ViewState["Folder"] = CurrNode;
ViewState["FileType"] = "All";
GetFilesFromFolder();
}
private void GetFilesFromFolder()
{
// GET A LIST OF FILES FROM A SPECIFILED FOLDER.
DirectoryInfo objDir = new DirectoryInfo(Server.MapPath((string)ViewState["Folder"]));
FileInfo[] listfiles = objDir.GetFiles("*." + ((string)ViewState["FileType"] != "All" ?
ViewState["FileType"] : "*"));
if (listfiles.Length > 0)
{
// BIND THE LIST OF FILES (IF ANY) WITH GRIDVIEW.
GridView1.Visible = true;
GridView1.DataSource = listfiles;
GridView1.DataBind();
lblMsg.Text = listfiles.Length + " files found";
}
else
{
GridView1.Visible = false;
lblMsg.Text = "No files found";
}
}
OnSelectedNodeChanged method get called on node click, and all value sets properly but never get reflect.
Please help.
It's your update panel. The node event originates within the UpdatePanel so only the Update panel will get updated after postback. Keep in mind that the full Page lifecycle occurs, so the gridview does get databound, but only the content within the UpdatePanel will be refreshed.
Your options:
Add Folder_Treeview as a PostBack Trigger -or-
Get rid of the UpdatePanel altogether -or-
Move the Gridview inside the UpdatePanel ContentTemplate
Also a TreeView is one of several server controls that may not be compatible with an UpdatePanel:
The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:
TreeView control under several conditions. One is when callbacks are enabled that are not part of an asynchronous postback. Another is when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles. Another is when the EnableClientScript property is false (the default is true). Another is if you change the value of the EnableClientScript property between asynchronous postbacks. For more information, see TreeView Web Server Control Overview.
Menu control when you set styles directly as control properties instead of implicitly styling the control by using a reference to CSS styles. For more information, see Menu Control Overview.
FileUpload and HtmlInputFile controls when they are used to upload files as part of an asynchronous postback.
GridView and DetailsView controls when their EnableSortingAndPagingCallbacks property is set to true. The default is false.
Login, PasswordRecovery, ChangePassword, and CreateUserWizard controls whose contents have not been converted to editable templates.
The Substitution control.

How do I make a postback trigger concurrently?

I have a TextboxSectionName, TextboxSectioncode and Checkbox. When Checkbox gets checked, a text in the TextboxSectionname will be added into TextboxSectioncode, TextboxSectioncode will be read-only, and gets background color; When unchecked, TextboxsectionName gets back.
The problem is process above works fine, but if I try to type more text into textboxSectionName when TextboxSectionCode is still checked, TextboxSectionCode doesn't take a text that has been added to textboxSectionName. To get it working, I have to uncheck the checkbox and check it again.
Is there any way to make the TextboxSectionCode take newly-added text from TextboxSectionName?
Here I have this block of code.
aspx
<asp:CheckBox ID="CheckBoxSectionCode" runat="server" Style="margin-left: 5px;" OnCheckedChanged="CheckBoxSectionCode_CheckedChanged" AutoPostBack="True" />
<asp:TextBox ID="TextBoxSectionName" runat="server" Width="250px" onkeyup="RefreshUpdatePanel();" ></asp:TextBox>
<asp:TextBox ID="TextBoxSectionCode" runat="server" Width="250px" ></asp:TextBox>
code behind checkbox event handler
protected void CheckBoxSectionCode_CheckedChanged(object sender, EventArgs e)
{
if (CheckBoxSectionCode.Checked == true)
{
TextBoxSectionCode.Text = TextBoxSectionName.Text;
TextBoxSectionCode.BackColor = Color.LightGray;
TextBoxSectionCode.ReadOnly = true;
}
else
{
TextBoxSectionCode.ReadOnly = false;
TextBoxSectionCode.Text = "";
TextBoxSectionCode.BackColor = Color.White;
}
}
Thanks,
Phillip

If statement on dropDownList listItem

I need to add a condition to a DropDownList where a method can be executed by button click only if the user has selected a value different than the listItem (default value).
<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"
DataSourceID="SqlDataSource5"
DataTextField="proj_name" DataValueField="proj_name">
<asp:ListItem Text="Select a project to clone" Value="" />
</asp:DropDownList>
How can I structure an if condition to validate that the selected value is not the ListItem (default value)?
You can use asp.net delivered validation controls
Ex:
<asp:RequiredFieldValidator id="rfv1"
ControlToValidate="DropDownList1"
Display="Static"
ErrorMessage="* Select a value"
InitialValue="DefaultValueHere"
runat="server"
ValidationGroup="V1"/>
Then edit your button markup to use ValidationGroup
<asp:Button Id="button1" ValidationGroup="V1" .../>
In your codebehind button click code add this
protected void button1_onlick(Object sender, EventArgs e)
{
If(Page.IsValid)
{
// your existing code here
}
}
See sample code below
if (DropDownList1.SelectValue == "")
{
// Write your code here
}
you can also have:
if (DropDownList1.Text == "Select a project to clone")
{
// Write your code here
}

adding option to drop down list

I have a drop down list with multiple values. I have a list item named "Other". When selecting other I want to generate a text box with required field validator. I have written like this:
Markup:
<asp:DropDownList ID="dl_test_name" runat="server"
OnSelectedIndexChanged="SelectedIndexChanged"
Height="22px" Width="103px">
<asp:ListItem>Science</asp:ListItem>
<asp:ListItem>Maths</asp:ListItem>
<asp:ListItem>Other</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="tb_other" runat="server" Width="94px" Visible="False">
</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server"
ControlToValidate="tb_other" ErrorMessage="*">
</asp:RequiredFieldValidator>
Code-behind:
protected void SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList dropDownList = (DropDownList)sender;
if (dropDownList.SelectedValue == "Other")
{
tb_other.Enabled = true;
tb_other.Text = string.Empty;
tb_other.Visible = true;
}
else
{
tb_other.Enabled = false;
tb_other.Text = dropDownList.SelectedValue;
}
}
but when selecting on any list item ,control doesn't go to SelectectedIndexChanged event. Only after reloading the page does it work.
What is the problem?
To make your DropDownList post back to the server you need to use the AutoPostback property, like this:
<asp:DropDownList ID="dl_test_name" runat="server"
OnSelectedIndexChanged="SelectedIndexChanged"
Height="22px" Width="103px" AutoPostBack="true">
Arathy, make AutopostBack property of dropdown to true in aspx

Change value of label and hide imagebutton in asp repeater

I've an asp repeater which has some fields inside an ItemTemplate. Each item in the repeater has an "add to cart" asp:ImageButton and an invisible asp:Label as well. The code looks like this:
<asp:Repeater ID="Repeater1" runat="server" OnItemCommand="addToCart">
<HeaderTemplate>
<table id="displayTable">
</HeaderTemplate>
<ItemTemplate>
<td>
<!-- fields like name, description etc in the repeater are present; i've omitted to show them here-->
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
</td>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
When a particular ImageButton in the repeater is clicked, I'm trying to display "added to cart" as the text of its corresponding Label, and make the clicked ImageButton Visible=false. I've tried using the OnItemCommand function for the ASP:Repeater. The method is "addToCart":
<>
void addToCart(Object Sender, RepeaterCommandEventArgs e)
{
Cart cart = new Cart();
cart.instrument_id = //id of product from repeater based on user click
String userName = Membership.GetUser().ToString();
cart.user_name = userName;
cart.quantity = 1;
var thisLbl = (Label)e.Item.FindControl("addedToCartLabel");
var thisImg = (ImageButton)e.Item.FindControl("addToCartImg");
try
{
database.Carts.InsertOnSubmit(cart);
database.SubmitChanges();
thisImg.Visible = false;
thisLbl.Text = "Added to Cart!";
thisLbl.Visible = true;
}
catch (Exception ex)
{
thisImg.Visible = false;
thisLbl.Text = "Processing failed;please try again later";
thisLbl.Visible = true; ;
}
}
The aspx page is populated properly. However, when I click on any of the ImageButtons in the repeater, I get the following error:
Server Error in '/mysite' Application.
Invalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them.
If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback
or callback data for validation.
Can someone help me with this?
May I suggest doing this with client-side javascript rather than a server-side call?
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" ImageUrl="hi.jpg" Width="75px" Height="50px" />
becomes
<asp:Label ID="addedToCartLabel" runat="server" Visible="false"></asp:Label>
<asp:ImageButton ID="addToCartImg" runat="server" onclick="javascript:function() { this.this.style.display='none'; document.getElementById(this.parentNode.firstChild.id).style.display='block'; }" ImageUrl="hi.jpg" Width="75px" Height="50px" />

Categories

Resources