TreeList.SelectedItems showing only current page selected items in telerik - asp.net

I am using Telerik Radtreelist controls here is the markup
<telerik:RadTreeList ID="rtreeList_Topic" runat="server" OnNeedDataSource="rtreeList_Topic_NeedDataSource"
ParentDataKeyNames="parent_topicid" DataKeyNames="topicid" AllowPaging="true" RenderMode="Classic" Skin="WebBlue"
AutoGenerateColumns="false" AllowSorting="true"
OnDeleteCommand="rtreeList_Topic_OnDeleteCommand" OnInsertCommand="rtreeList_Topic_OnInsertCommand"
OnUpdateCommand="rtreeList_Topic_OnUpdateCommand" AllowMultiItemSelection="True"
OnItemDataBound="rtreeList_Topic_OnItemDataBound" HeaderStyle-Height="35px">
<Columns>
<telerik:TreeListSelectColumn HeaderStyle-Width="25px">
</telerik:TreeListSelectColumn>
<telerik:TreeListTemplateColumn HeaderText="Topic ID" HeaderStyle-Width="40px">
<ItemTemplate>
<asp:HyperLink ID="targetControl" runat="server" NavigateUrl="#" Text='<%# Eval("topicid") %>'></asp:HyperLink>
</ItemTemplate>
</telerik:TreeListTemplateColumn>
<telerik:TreeListBoundColumn DataField="parent_topicid" UniqueName="parent_topicid" HeaderText="Parent Topic Id" Visible="False">
</telerik:TreeListBoundColumn>
Here, you can see I have a TreeListSelectColumn and after selection I want to get each selected TreeListDataItem..
for obvious reason I could do iterate over Radtreelistselected item
foreach(var item in rtreeList_Topic.SelectedItems)
{
var dr = dtselected.NewRow();
dr["parent_topic_id"] = item["parent_topicid"].Text.Contains(" ") ? DBNull.Value : (object) Convert.ToInt32(item["parent_topicid"].Text);
dr["subject_code"] = rcb_subject_code.SelectedItem.Text;
dr["topic_id"] = Convert.ToInt32(item["topicid"].Text);
dr["topic_description"] = item["topicname"].Text;
dr["topic_shortname"] = item["ShortName"].Text;
dtselected.Rows.Add(dr);
}
Now say on First page(in footer you can have pages) have selected 4 items and on second page I have selected 2 items.
so rtreeList_Topic.SelectedItems.Count = 6 ? but no it showing only second page's selected item.. why? If there is any problem in my code please let me know.. Thanks :)

Related

find control not working gridview

I have a page which contains gridview and button now i want to add gridview values to database table when i click on button, but when i click on button and try to get value using find control its not showing any value it shows value as blank.
my code is given below
aspx page
<%# Page Title="" Language="C#" MasterPageFile="Masters.Master" AutoEventWireup="true" EnableEventValidation ="false" CodeBehind="test.aspx.cs" Inherits="test" UICulture="hi-IN" Culture="hi-IN" %>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnDataBound="GridView1_DataBound"
OnRowDataBound="GridView1_RowDataBound" BackColor="White" BorderColor="White"
BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical">
<asp:TemplateField Visible="true">
<HeaderTemplate>
<asp:Label ID="lbl4" runat="server" Text="test."></asp:Label>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="lbltestno" runat="server" Text='<%# Bind("testing") %>' Visible="true"></asp:Label><br style="mso-data-placement:same-cell;"/>
</ItemTemplate>
</asp:TemplateField>
.cs page
on button click
foreach (GridViewRow GvRow in GridView1.Rows)
{
Label lblvinnos = (Label)GvRow.FindControl("lbltestno");
}
Please note that this page is working fine when i use without master page but when i am using master page its showing find control values as null
I just checked its finding textbox value not of labels any particular reason?
Try in Button cliked event like:
int indexcount = 0;
foreach (GridViewRow row in GridView1.Rows)
{
// get value from label on each row in gridview
string value = (GridView1.Rows[indexcount].FindControl("lbltestno") as Label).Text;
indexcount++;
}
You have to find it in cell, cell_index is actual index (starts with 0) of cell where control resides.
Label lblvinnos = (Label)GvRow.Cells[cell_index].FindControl("lbltestno");
create button inside a template field
trigger it inside your gridview_rowcommand method
applies per row only
GridViewRow row = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
string your_variable = Convert.ToDecimal(((TextBox)row.FindControl("your control here")).Text);
hope it helps

Add dynamic radio button object to GridView

I need to add a custom radio button control that I created based on an if condition to my GridView. My radiobutton will be enabled or disabled based on this condition and will have the text
changed as well.
I'm trying to figure out how to add a radiobutton object into my data row instead of a string dt.Columns.Add("FirstName").
<telerik:RadGrid runat="server" ID="grd1" OnNeedDataSource="grd1_NeedDataSource">
<MasterTableView AutoGenerateColumns="False">
<Columns>
<telerik:GridTemplateColumn HeaderText="Radiobutton header" UniqueName="col1">
<ItemTemplate>
<asp:RadioButton ID="rbType" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "rbEnableorDisable")%>' />
</ItemTemplate>
</telerik:GridTemplateColumn>
<telerik:GridTemplateColumn HeaderText="FirstName header" UniqueName="col2">
<ItemTemplate>
<asp:Label Text='<%# DataBinder.Eval(Container.DataItem, "Name")%>' runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>
</Columns>
</MasterTableView>
</telerik:RadGrid>
CodeBehind
Private dt As DataTable
Private dr As DataRow
dt= New DataTable
dt.Columns.Add("rbEnableorDisable")
dt.Columns.Add("FirstName")
Dim rb As RadioButton
rb = New RadioButton
For each item in itemlist //some data iteration declared elsewhere
dr = dt.NewRow()
If (Condition)
rb.Text = "Should be Disabled"
rb.Enabled = False
Else
rb.Text = "Should be Enabled"
rb.Enabled = True
End if
dr.Item("FirstName") = item.FirstName
dr.Item("rbEnableOrDisable") = rb//?Code for inserting a radio button object
dt.Rows.Add(dr)
Next
With grd1
.DataSource = dt
.DataBind()
End With
So far with this code
I am only able to display the radiobutton text if i have dr.Item("rbEnableOrDisable") = rb.Text.
I need to display the whole radiobutton object(show the text and if it's enabled or disabled among others)
I tried
LocationData.Columns.Add(New DataColumn("rbType", GetType(RadioButton)))
but it seems I need to append to the ItemTemplate
Also tried adding the whole column dynamic with:
grd1.Controls.Add(rb)
You need to have something in your DataItem to put the radiobutton enabled or disabled and assigment to Enabled property.
Enabled='<%# DataBinder.Eval(Container.DataItem, "booleanData")%>'
If you need to put RadioButton checked or unchecked use Checked property.

GridView loses column contents during PostBack

I am having an issue with the behavior of a GridView between post backs.
The real problem comes from a TemplateField I define in the markup at column[0] with a child CheckBox control. Things work fine for the first, and second search executions. However, at some point between the second execution and anything that causes a post back there after, I lose the contents of the TemplateField.
Its only the the contents of the column and not the whole column itself that gets removed. The TemplateField is present in the source and shows a formated column at position 0 of the table.
CODE:
protected void ExecuteSearch(object sender, EventArgs e)
{
if (lb_SelectedFields.Items.Count == 0) { return; } //if no selected fields
//Generates custom SQL query based on user inputs and column Selections
BuildQuery(); // sets txbSqlText.Text = to the SQL string
DataTable Table = SqlAdapter.Select(new System.Data.SqlClient.SqlCommand(txbSqlText.Text));
for (int i = gv_SearchResults.Columns.Count - 1; i > 0; i--)
{ gv_SearchResults.Columns.RemoveAt(i); } //removes all the columns except[0]
foreach (ListItem Item in lb_SelectedFields.Items) //adds all the user defined columns
{
//Column object that is able to find the column definition
Column Col = ColumnsBasedOnFocus.FindColumName(Item.Value);
if (Col.Type == "HyperLink") { gv_SearchResults.Columns.Add(CreateHyperLinkField(Col)); }
else { gv_SearchResults.Columns.Add(CreateBoundColumn(Col, true)); } //true is if the column is visable
}
gv_SearchResults.DataSource = Table;
gv_SearchResults.DataBind();
}
ASP.NET:
<asp:GridView ID="gv_SearchResults" runat="server" GridLines="None" CellSpacing="0"
CellPadding="0" AutoGenerateColumns="false" CssClass="TABLE_LIGHTBLUE" Width="100%">
<HeaderStyle CssClass="TABLE_LIGHTBLUE_HEADERROW" />
<Columns>
<asp:TemplateField ItemStyle-Width="30" ItemStyle-Wrap="false">
<HeaderTemplate>
<center>
<asp:Button ID="btn_SelectAll" runat="server" OnClick="SelectAll" Text="All" CssClass="TEXT_SMALL" />
<asp:CheckBox ID="chk_Placeholder" runat="server" Visible="false" /></center>
</HeaderTemplate>
<ItemTemplate>
<center>
<asp:CheckBox ID="chk_Select" runat="server" Visible="true" />
<asp:Label ID="lbl_AssetGID" runat="server" Visible="false" Text='<%# Bind("i_GID") %>' /></center>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Found a link that describes a similar situation.
https://connect.microsoft.com/VisualStudio/feedback/details/104994/templatefield-in-a-gridview-doesnt-have-its-viewstate-restored-when-boundfields-are-inserted#details
They describe a bug in ASP.Net code that fails to properly manage view states with template fields in dynamically generated grid views.
Basically TemplateFields can't be properly restored from ViewState, and if you modify the ASPX-declared columns programmatically, it can't create them from the declarations either.
The only solution I could get working was to create a new class deriving from TemplateField that in the constructor set the ItemTemplate to an ITemplate-derived class, which means having to define the template programmatically instead of declaratively.
You can also rebind the gridview on each postback but that's its own can of worms.

problem in gridview control of user control

My problem is I am setting the datasource from parent page.But if I set disable to certain columns of gridview the event of the controls inside those disabled column template gets fired. Like I have a checkbox in one column, if disable that column from parent page while data binding the check_checked event is being fired.
here my code- user control aspx
<asp:TemplateField HeaderText="Exclude Null" ItemStyle-Width="50px">
<HeaderTemplate>
Exclude Null
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox runat="server" ID="chkExNull" OnCheckedChanged="chkExNull_OnCheckedChanged"
AutoPostBack="true" />
</ItemTemplate>
<ItemStyle HorizontalAlign="left" VerticalAlign="Top" />
<HeaderStyle HorizontalAlign="left" VerticalAlign="Top" />
</asp:TemplateField>
.cs file of parent
GridView gvCondition = (GridView)ucCondition.FindControl("gvCondition");
gvCondition.DataSource = ConditionFieldCollection;
gvCondition.Columns[5].Visible = false;
gvCondition.Columns[6].Visible = false;
gvCondition.Columns[7].Visible = false;
gvCondition.Columns[8].Visible = false;
gvCondition.DataBind();
What should I do, and is there any other way through which I can hide some of the columns of grid view control of user control??
Before your DataBind(). write this:
gvCondition.DataBound += new EventHandler(gvwCondition_DataBound);
and write in that method, the code to hide the columns.
Hope that helps.

Spliting data in Asp.Net Gridview

I Have a set of choices(options) coming from Database(around 36 records) which I have to insert in Gridview in such a way that 18 will go one column and 18 in another. And As these are choices the columns are needs to be a check box columns. So What I did is I have created a data table with 2 columns and split the data accordingly and then bind it to the grid.But the problem is If the question list is having odd no of items count ie 37. It is adding a blank record with check box in my gridview. Your help will be appreciated...
see code below
In my Aspx.cs
DataTable dTable = new DataTable();
dTable.Columns.Add("Questionsclmn1", typeof(string));
dTable.Columns.Add("Questionsclmn2", typeof(string));
for (int item = 0; item < QuestionList.Count; item = item + 2)
{
DataRow drow = dTable.NewRow();
drow["Questionsclmn1"] = QuestionList[item].Question;
if ((item + 1) < QuestionList.Count)
drow["Questionsclmn2"] = QuestionList[item + 1].Question;
dTable.Rows.Add(drow);
}
GrdVwQuestionsList.DataSource = dTable;
GrdVwQuestionsList.DataBind();
In my Aspx file under gridview
<Columns>
<asp:TemplateField HeaderText="Please Choose the Options Below">
<ItemTemplate>
<asp:CheckBox ID="chkQuestionList1" runat="server"
Text='<%# DataBinder.Eval( Container.DataItem, "Questionsclmn1")%>'/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkQuestionList2" runat="server"
Text='<%# DataBinder.Eval( Container.DataItem, "Questionsclmn2")%>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
Thanks in Advance.
Regards,
Chetan
I wouldn't use a GridView for this at all. CheckboxList can lay out checkboxes in two columns automatically.
<asp:CheckboxList runat="server" RepeatLayout="Table" RepeatColumns="2" RepeatDirection="Vertical" ... />
There's also DataList that supports the same properties but lets you use a template for the content.

Resources