Hello,
I have a dropdown, 2 link buttons(Lets say A and B) and a radgrid on my mark up page. The dropdown will select my DataSource for the grid. LinkButton A will get the data source and and adds the columns to the grid programmatically and binds them all. The other(LinkButton B) will get the datafrom the grid and do something different(not related to the page), anyway it will create a postback. The columns will be different depending on the selected value of the dropdown as is the data source. I am able to bind the data and columns on the grid programmatically. But when I click on LinkButton B, The columns go missing but the data is still there. As the header texts are missing. (Check the images attached). Here is my code:
ASP Page
C# Code :
I saw this post as well. The RadGrid is created on Page_Init and then added. But I want to keep the mark up and only bind the data and columns through server side. I tried using Rebind() and DataBind() function on 'LnkBtnB_OnClick', Then all the data disappears. I do not want to Bind the data on every post back.
If theres a way to make the columns not to disappear then its great. I am also okay if theres a fix to bind only the columns as well. But not to bind all the data.
Thank you
I have a dropdown, 2 link buttons(Lets say A and B) and a radgrid on my mark up page. The dropdown will select my DataSource for the grid. LinkButton A will get the data source and and adds the columns to the grid programmatically and binds them all. The other(LinkButton B) will get the datafrom the grid and do something different(not related to the page), anyway it will create a postback. The columns will be different depending on the selected value of the dropdown as is the data source. I am able to bind the data and columns on the grid programmatically. But when I click on LinkButton B, The columns go missing but the data is still there. As the header texts are missing. (Check the images attached). Here is my code:
ASP Page
<
asp:DropDownList
ID
=
"ddlDataSource"
runat
=
"server"
></
asp:DropDownList
>
<
asp:LinkButton
ID
=
"lnkBtnA"
runat
=
"server"
Text
=
"Button A"
OnClick
=
"LnkBtnA_OnClick"
></
asp:LinkButton
>
<
asp:LinkButton
ID
=
"lnkBtnB"
runat
=
"server"
Text
=
"Button B"
OnClick
=
"LnkBtnB_OnClick"
></
asp:LinkButton
>
<
telerik:RadGrid
ID
=
"radGrid1"
runat
=
"server"
>
<
ExportSettings
OpenInNewWindow
=
"true"
ExportOnlyData
=
"true"
IgnorePaging
=
"true"
>
</
ExportSettings
>
<
ClientSettings
>
<
Scrolling
AllowScroll
=
"true"
UseStaticHeaders
=
"true"
ScrollHeight
=
"200px"
/>
</
ClientSettings
>
<
MasterTableView
Width
=
"100%"
HorizontalAlign
=
"NotSet"
AutoGenerateColumns
=
"False"
NoMasterRecordsText
=
"No Items available"
>
<
Columns
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
C# Code :
protected
void
LnkBtnA_OnClick(
object
sender, EventArgs e)
{
try
{
this
.radGrid1.Visible =
true
;
this
.radGrid1.MasterTableView.Columns.Clear();
switch
(
this
.ddlDataSource.SelectedValue)
{
case
"DataSource1"
:
this
.AddColumnsToGrid();
this
.radGrid1.DataSource = MyDataSource1;
this
.radGrid1.DataBind();
break
;
case
"DataSource2"
:
this
.AddColumnsToGrid();
this
.radGrid1.DataSource = MyDataSource2;
this
.radGrid1.DataBind();
break
;
default
:
break
;
}
}
catch
(Exception ex)
{
throw
;
}
}
private
void
AddColumnsToGrid()
{
this
.radGrid1.MasterTableView.Columns.Clear();
switch
(
this
.ddlDataSource.SelectedValue)
{
case
"DataSource1"
:
GridBoundColumn subjectColumnR1 =
new
GridBoundColumn();
subjectColumnR1.DataField =
"Subject"
;
subjectColumnR1.HeaderText =
"Subject"
;
this
.radGrid1.MasterTableView.Columns.Add(subjectColumnR1);
GridBoundColumn dueDateColumnR1 =
new
GridBoundColumn();
dueDateColumnR1.DataField =
"DueDateTime"
;
dueDateColumnR1.HeaderText =
"DueDateTime"
;
this
.radGrid1.MasterTableView.Columns.Add(dueDateColumnR1);
GridBoundColumn ownerColumnR1 =
new
GridBoundColumn();
ownerColumnR1.DataField =
"Owner"
;
ownerColumnR1.HeaderText =
"Owner"
;
this
.radGrid1.MasterTableView.Columns.Add(ownerColumnR1);
break
;
case
"DataSource2"
:
GridBoundColumn subjectColumnR2 =
new
GridBoundColumn();
subjectColumnR2.DataField =
"Subject"
;
subjectColumnR2.HeaderText =
"Subject"
;
this
.radGrid1.MasterTableView.Columns.Add(subjectColumnR2);
GridBoundColumn dueDateColumnR2 =
new
GridBoundColumn();
dueDateColumnR2.DataField =
"DueDateTime"
;
dueDateColumnR2.HeaderText =
"DueDateTime"
;
this
.radGrid1.MasterTableView.Columns.Add(dueDateColumnR2);
GridBoundColumn ownerColumnR2 =
new
GridBoundColumn();
ownerColumnR2.DataField =
"Manager"
;
ownerColumnR2.HeaderText =
"Manager"
;
this
.radGrid1.MasterTableView.Columns.Add(ownerColumnR2);
break
;
default
:
break
;
}
}
protected
void
LnkBtnB_OnClick(
object
sender, EventArgs e)
{
try
{
this
.AddColumnsToGrid();
// <-- Even though I do this. The columns go missing.
// Does something not related to the question
}
catch
(Exception ex)
{
throw
;
}
}
I saw this post as well. The RadGrid is created on Page_Init and then added. But I want to keep the mark up and only bind the data and columns through server side. I tried using Rebind() and DataBind() function on 'LnkBtnB_OnClick', Then all the data disappears. I do not want to Bind the data on every post back.
If theres a way to make the columns not to disappear then its great. I am also okay if theres a fix to bind only the columns as well. But not to bind all the data.
Thank you