Programmatically binding with NeedDatasource? - c#

First Issue,I have two controls (a radcombox and a Treelist controls) on my page. On radcombobox's SelectedIndexChanged I am populating data from database.Now I want to fire NeedDatasource event on SelectedIndexChanged. How could I do that ?
Second issue,
As I am facing some issues with needdatasource, I have manually binded using databind method of radtreelist.After binding it shows only parent nodes at first.
After clicking on PageSize combo of RadTreelist, it loads with respective child nodes.Why is this happening here ?
<telerik:RadComboBox runat="server" ID="rcb_testtype" AutoPostBack="True" OnSelectedIndexChanged="rcb_testtype_OnSelectedIndexChanged">
<Items>
<telerik:RadComboBoxItem runat="server" Text="Select a Test Type" Value="-1" />
<telerik:RadComboBoxItem runat="server" Text="Practise Test" Value="pt" />
<telerik:RadComboBoxItem runat="server" Text="Normal Test" Value="nt" />
</Items>
</telerik:RadComboBox>
<telerik:RadTreeList ID="rtl_specific_topic" runat="server"
ParentDataKeyNames="parent_topicid" DataKeyNames="topicid" AllowPaging="true" RenderMode="Classic" Skin="WebBlue"
AutoGenerateColumns="false" AllowSorting="true" ExpandCollapseMode="Client" AllowRecursiveSelection="False" OnItemDataBound="rtl_specific_topic_OnItemDataBound"
AllowMultiItemSelection="true" OnItemCommand="RadTreeList1_ItemCommand" OnPageSizeChanged="RadTreeList1_PageSizeChanged" OnPageIndexChanged="RadTreeList1_PageIndexChanged">
<Columns>
<telerik:TreeListSelectColumn HeaderStyle-Width="38px">
</telerik:TreeListSelectColumn>
<telerik:TreeListBoundColumn DataField="parent_topicid" UniqueName="parent_topicid" HeaderText="Parent Topic Id" Visible="False">
</telerik:TreeListBoundColumn>
<telerik:TreeListBoundColumn DataField="topicid" UniqueName="topicid" HeaderText="Topic ID" Visible="False">
ion" UniqueName="description" HeaderText="Topic Name">
</telerik:TreeListBoundColumn>
<telerik:TreeListTemplateColumn HeaderText="Weightage" UniqueName="syllabus_weightage" HeaderStyle-Width="95px" ItemStyle-Width="95px">
<ItemTemplate>
<telerik:RadNumericTextBox runat="server" ID="rntb_weightage" MinValue="0" Width="80px" MaxValue="100" EmptyMessage="weightage(%)" AllowRounding="true"></telerik:RadNumericTextBox>
</ItemTemplate>
</telerik:TreeListTemplateColumn>
<telerik:TreeListTemplateColumn HeaderText="Weightage" UniqueName="quest_category">
<ItemTemplate>
<asp:DropDownList ID="ddl_quest_category" runat="server" Width="100px" style="right: 1px;">
<asp:ListItem Text="select a question type" Value="-1"></asp:ListItem>
<asp:ListItem Text="Multiple Choice" Value="1"></asp:ListItem>
<asp:ListItem Text="True/False" Value="2"></asp:ListItem>
<asp:ListItem Text="Essay Writing" Value="3"></asp:ListItem>
<asp:ListItem Text="Pictorial" Value="4"></asp:ListItem>
<asp:ListItem Text="Short Question" Value="5"></asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<HeaderStyle Width="90px"></HeaderStyle>
</telerik:TreeListTemplateColumn>
<telerik:TreeListTemplateColumn HeaderText="Set Level" UniqueName="level">
<ItemTemplate>
<telerik:RadSlider runat="server" ID="rs_level" Skin="Web20" Width="490px" AutoPostBack="True"
Height="70px" CssClass="dragRangeSlider" EnableServerSideRendering="true" IsSelectionRangeEnabled="true" OnValueChanged="rs_level_OnValueChanged"
EnableDragRange="true"
ItemType="Item">
<Items>
<telerik:RadSliderItem Text="L1" Value="1" runat="server"></telerik:RadSliderItem>
<telerik:RadSliderItem Text="L2" Value="2" runat="server"></telerik:RadSliderItem>
</Items>
</telerik:RadSlider>
</ItemTemplate>
<HeaderStyle Width="490px"></HeaderStyle>
<ItemStyle Width="490px"></ItemStyle>
</telerik:TreeListTemplateColumn>
</Columns>
</telerik:RadTreeList>
OnSelectedIndexChanged bind radtreelist
in Code behind
protected void rcb_testtype_OnSelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
rcb_syllabus_name.Visible = rcb_testtype.SelectedValue != "pt";
rtl_specific_topic.DataSource = FetchTopicDetailsForSyllabus();
rtl_specific_topic.DataBind();
}
private DataTable FetchTopicDetailsForSyllabus()
{
DataTable dtTopocsForTest = null;
if (rcb_testtype.SelectedValue == "nt" && rcb_syllabus_name.SelectedValue != "")
{
obj_BEL_LMS.Flag = "normaltest";
obj_BEL_LMS.Syllabusid = Convert.ToInt32(rcb_syllabus_name.SelectedValue);
dtTopocsForTest = obj_BL_LMS.FetchSyllabusDetails(obj_BEL_LMS);
}
else if (rcb_testtype.SelectedValue == "pt")
{
obj_BEL_LMS.iFlag = 2;
obj_BEL_LMS.iBranchId = Convert.ToInt32(Session["branchid"]);
dtTopocsForTest = obj_BL_LMS.FetchTopicDetailsDb(obj_BEL_LMS);
}
return dtTopocsForTest;
}
After binding it shows only parent data.

From the Telerik documentation:
This event fires in the following cases:
Right after OnLoad, Telerik RadTreeList checks the viewstate for stored TreeList-related information. If such information is missing (when the page loads for the first time), the NeedDataSource event is fired. This also means that if the EnableViewState property of the control has been set to false, the treelist will bind each time the page loads (not only the first time)
After expand/collapse
When paging event occurs
When other operations requiring Rebind occurs
The advantages of using this event are that the developer does not need to write any code handling the logic about when and how the data-binding should be processed. It is still the developer's responsibility to construct properly a data source object and assign it to the RadTreeList's DataSource property.
In the code of the NeedDataSource handler you should prepare the data source (list of objects) for Telerik RadTreeList and assign it to the grid's DataSource property. Also you should set the DataKeyNames and ParentDataKeyNames propertied for the TreeList control.
Note: You should never call the DataBind() method from inside the NeedDataSource handler or mix simple data-binding mode with advanced data-binding
I suggest you edit your post and include your code.

Related

Invalid Postback On Server Populated DropDownLists

I've seen multiple variations of this question asked, but all of the answers seem to involve javascript.
I'm querying a database, retrieving a list of items, and populating 3 different dropdown lists with the exact same content. After I select an item from a list, and click the "Save" button, I get an invalid postback error.
I'm certain it's because I'm using AutoPostBack on my DropDownList to trigger an update and disable a new selection in the other lists. When I set AutoPostBack to false, the error disappears.
I've read that I need to register my list content for event validation, but the examples I'm seeing, as mentioned above, are manually entered or handled with javascript; not server side from content generated by a DB query.
The solution I'm using now is a "link button" instead of a regular asp:button, since the former does not seem to cause a postback.
I would like to know if there is a more formal way of handling this. I'm not going to disable event validation, for security reasons.
<asp:UpdatePanel ID="DropDownListPanel" runat="server"
ChildrenAsTriggers="true" ><ContentTemplate>
<asp:gridview GridLines="Both" runat="server"
ShowHeader="false" ShowFooter="true" HorizontalAlign="Left"
AutoGenerateColumns="false">
<Columns>
<asp:TemplateField ControlStyle-Height="100%" ItemStyle-Width="25%">
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
AppendDataBoundItems="true" Width="100%" AutoPostBack="true"
OnSelectedIndexChanged="CheckItem">
<asp:ListItem Value="-1">Select</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</Columns>
</asp:gridview>
</ContentTemplate>
</asp:UpdatePanel>
<asp:Button ID="SaveButton" runat="server" Text="Save"
Enabled="True" OnClick="SaveTable" />

How to set selected value of an asp:dropdownlist in a AspxGridView

I checked around for a solution to this and the solutions I have found use a SqlDataSource, but I am not populating it that way. I have the items hardcoded in the html, and the grid is binded to a dataset.
This is my first time using dropdowns in any type of grid and I am getting confused. I tried using the ComboBoxColumn in the item template but was having major issues on trying to find the combobox control, so I went with the normal asp:DropDownList. Incase you're wondering, I can't even find that control without it returning null.
So as the grid gets populated I need to set the selected value of the dropdowns.
The markup is
<dx:ASPxGridView ID="xgvEdit" runat="server" Width="100%">
<Columns>
<dx:GridViewDataColumn FieldName="roleID" Caption="ID" Visible="false"></dx:GridViewDataColumn>
<dx:GridViewDataColumn FieldName="modulID" Caption="Document/UseCase (Right Object)">
<Settings AutoFilterCondition="Contains" />
</dx:GridViewDataColumn>
<dx:GridViewDataColumn FieldName="right_level" Caption="Right Level">
<DataItemTemplate>
<asp:DropDownList ID="ddRightLevel" runat="server" AutoPostBack="false">
<asp:ListItem Text="No Right" Value="0" />
<asp:ListItem Text="Read" Value="1" />
<asp:ListItem Text="Write" Value="2" />
<asp:ListItem Text="Execute" Value="3" />
</asp:DropDownList>
</DataItemTemplate>
</dx:GridViewDataColumn>
<dx:GridViewDataColumn FieldName="comments" Caption="Comments">
<Settings AutoFilterCondition="Contains" />
</dx:GridViewDataColumn>
</Columns>
I suppose that this is custom GridView which inherits asp:GridView. You need to add event
OnRowDataBound="Grid_RowDataBound"
In Code behind:
protected void ProductGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.DataItem == null)
return;
DropDownList ddl= e.Row.FindControl("ddRightLevel") as DropDownList;
//do stuff
}

ASP.Net DropDownList in GridView - Codebehind always sees 1st item as selected

I am working on an ASP.Net application with code behind as C#. In an aspx page, there is a gridview control. One column contains dropdownlist, as follows:
<asp:GridView ID="gvModulesSem1" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Grade" HeaderStyle-HorizontalAlign="Left" HeaderStyle-CssClass="gvModulesTextPadding">
<ItemStyle Width="120px" CSSClass="gvModulesTextPadding" />
<ItemTemplate>
<asp:DropDownList ID="ddlSelectGrade" Text='<%# Eval("Grade")%>' runat="server">
<asp:ListItem Value="-1.0">Select Grade</asp:ListItem>
<asp:ListItem Value="Distinction">Distinction</asp:ListItem>
<asp:ListItem Value="A">A</asp:ListItem>
<asp:ListItem Value="B+">B+</asp:ListItem>
<asp:ListItem Value="B">B</asp:ListItem>
<asp:ListItem Value="C+">C+</asp:ListItem>
<asp:ListItem Value="C">C</asp:ListItem>
<asp:ListItem Value="D+">D+</asp:ListItem>
<asp:ListItem Value="D">D</asp:ListItem>
<asp:ListItem Value="P">P</asp:ListItem>
<asp:ListItem Value="F">F</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
There are some other columns too, all of which contain labels. But, to keep it simple, I have removed the codes for the other columns.
In page load, when Page.IsPostBack is false, the GridView is bound to a DataTable (which has been filled). This populates the GridView. All the DropDownLists have the first item i.e. "Select Grade" selected.
As a user, I change the selected items of the dropdown lists. Then click a button (outside the grid). In the button click event procedure, when I try to read the selected item of any dropdown list, I find that it is always the first item.
How do I correct this problem?
Thank you.

Execute Update on DropDownList change

At the moment, I have a GridView with the following in the ItemTemplate:
<asp:LinkButton ID="lbEdit" runat="server" CommandName="Edit">
<asp:Label ID="Label6x" runat="server" Text='<%# Bind("progress_full") %>' /></asp:LinkButton>
This works fine and when I click on it, it displays the EditTemplate which currently contains the following:
<asp:DropDownList ID="DropDownList3" runat="server"
SelectedValue='<%# Bind("progress") %>'>
<asp:ListItem Value="0">In queue</asp:ListItem>
<asp:ListItem Value="1">Being worked on</asp:ListItem>
<asp:ListItem Value="2">Complete</asp:ListItem>
</asp:DropDownList><br />
<asp:LinkButton ID="lbUpdate" runat="server" CommandName="Update" Text="Update" />
How can I get the dropdown to automatically execute the Update command when it is changed, instantly returning back to the ItemTemplate, instead of me making the change to the DropDown and having to click Update?
You add AutoPostBack="true" to your DropDownList and set OnSelecIndexChanged="DropDownList1_SelectedIndexChanged"
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//execute here your update
......
}

Gridview's update event firing even though data is invalid in controls

I have an commandfield and few templatefields. The templatefields have validators attached to it and they show up proper messages when wrong data is selected. However when I click commandfield no error is shown and the event fires even though data is invalid. Morover, I have also checked Page.IsValid on server and all works out well even though data is in invalid state. This the markup:
<asp:TemplateField HeaderText="Exp. Date">
<ItemTemplate>
<asp:Label ID="lblExpiration" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Expiration")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate >
<asp:DropDownList ID="ddlMM" runat="server" ValidationGroup="vgExpDateGrid">
<asp:ListItem Value="-1">MM</asp:ListItem>
<asp:ListItem Value="1" >01</asp:ListItem>
<asp:ListItem Value="2">02</asp:ListItem>
<asp:ListItem Value="3">03</asp:ListItem>
<asp:ListItem Value="4">04</asp:ListItem>
<asp:ListItem Value="5">05</asp:ListItem>
<asp:ListItem Value="6">06</asp:ListItem>
<asp:ListItem Value="7">07</asp:ListItem>
<asp:ListItem Value="8">08</asp:ListItem>
<asp:ListItem Value="9">09</asp:ListItem>
<asp:ListItem Value="10">10</asp:ListItem>
<asp:ListItem Value="11">11</asp:ListItem>
<asp:ListItem Value="12">12</asp:ListItem>
</asp:DropDownList>
<span class="green"></span> /
<asp:DropDownList ID="ddlYY" runat="server" ValidationGroup="vgExpDateGrid">
</asp:DropDownList>
<span class="green"></span>
<asp:RequiredFieldValidator ID="rfvddlMM" ControlToValidate="ddlMM" Display="Dynamic" InitialValue="-1" runat="server" ValidationGroup="vgExpDateGrid">*</asp:RequiredFieldValidator>
<asp:RequiredFieldValidator ID="rfvddlYY" ControlToValidate="ddlYY" Display="Dynamic" InitialValue="-1" runat="server" ValidationGroup="vgExpDateGrid">*</asp:RequiredFieldValidator>
</EditItemTemplate>
</asp:TemplateField>
<asp:CommandField
UpdateText="Update" CausesValidation="true"
HeaderText="Update" ShowEditButton="true"
EditText="Update <br/>Exp. Date" ButtonType="Link" />
Please do not pay attention to the weird control names and styles. Basically the template field has dropdownlists of year and month. Year's drop down list is populated in rowdatabound event. As you can see validators are attached still commandfield works normally. Can anybody tell me what can be the problem?
At first glance I would say you do not have ValidationGroup="vgExpDateGrid" on asp:CommandField. Change your commandfield to:
<asp:CommandField ValidationGroup="vgExpDateGrid"
UpdateText="Update" CausesValidation="true"
HeaderText="Update" ShowEditButton="true"
EditText="Update <br/>Exp. Date" ButtonType="Link" />
or alternatively remove ValidationGroup from asp:RequiredFieldValidator

Categories

Resources