data binding a datagrid in a UserControl

P

p.mc

Hi all,

I'm having major problems with a userControl which contains a datagrid.
My problem concerns data binding.

The Page_Load() procedure calls the DataBind procedure to bind the
datagrid to a DataSet.
If i include an if statement to prevent the data binding from occuring
on a page PostBack in the following way:

if (!IsPostBack){
DataBind();
}

the records are displayed correctly initially but the datagrid is not
populated (no data is shown at all) after the main page (containing the
user control) recieves a postback.

If i remove the if condition the data is displayed twice after an edit
or update event is envoked within the user control datagrid.
eg.
row1
row2
row3
row1
row2
row3

i've tried making the DataBind() procedure accessible to the containing
aspx page so that i can call it when the main page is reloaded but am
having problems accessing the procedure.

is this the right way to solve this problem? or am i looking in the
wrong direction?

any help would be much appreciated!!

thanks in advance, Paul
 
S

Swanand Mokashi

do you have EnableViewstate set to false for the datagrid ? set it to
true - -that might be a reason why it is not showing (maintining data in the
viewstate) on postback


--
Swanand Mokashi
Microsoft Certified Solution Developer (.NET) - Early Achiever
Microsoft Certified Application Developer (.NET)

http://www.dotnetgenerics.com/
DotNetGenerics.com -- anything and everything about Microsoft .NET
technology ...

http://www.swanandmokashi.com/
http://www.swanandmokashi.com/HomePage/WebServices/
Home of the Stock Quotes, Quote of the day and Horoscope web services
 
P

p.mc

I've tried explictly setting enableViewState to true but that's not
helped,

thanks for your help, any other posibilities you can think of?
 
P

p.mc

hi again

thanks for your help, my page design is getting a bit complicated (to
be honest it's a bit of a hack!) but i've tried to cut out the
non-relevant bits.

basically i've got a main page with a datagrid, this contains a user
control with a second datagrid. the first data grid shows only one
record per page, while the user control shows all related records
(basically a master detail view). However i've actually got two user
controls, one in the <itemTemplate> and a slight variation in the
<editTemplateItem> which has a editcommand column.

displaying the main page loads the first (display only) user control
fine, but entering edit mode just gives a blank datagrid. If i remove
the if(!IsPostBack) condition from the binding on the user control,
data is displayed but is duplicated as above.

sorry if i've repeated myself there, but that helped to get it clearer
in my own head!

here's my code then!

main page
=========

<asp:DataGrid ID="dgImage" runat="server" AutoGenerateColumns="false"
BorderWidth="0px" BorderColor="#FFFFFF" OnEditCommand="dg_Edit"
OnCancelCommand="dg_Cancel" OnUpdateCommand="dg_Update"
EnableViewState="true" >
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<table width="100%">
<tr>
<td class="dataCell">
<ioeKW:subjects id="dgSubject" runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td colspan="3" class="dataCell">
<ioeKW:subjects_edit id="dgSubjectEdit" runat="server" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:Datagrid>

UserControl 1
=============

<script runat="server" language="c#">

private string imgID;
private SqlConnection objConn = new SqlConnection("Server=SATURN;" +
"Database=Images_of_Empire;" + "User ID=Empire;Password=Empire");
private SqlDataAdapter objDA_subject;
private DataSet objDS_subject = new DataSet();

private void Page_Load() {
BindData();
}

private void BindData() {
imgID=Request.Cookies["ioe_imageID"].Value;

objDA_subject = new SqlDataAdapter("SELECT *,
kw_subject_lv1.subjectLv1_desc, kw_subject_lv2.subjectLv2_desc FROM
junct_subject INNER JOIN dbo.kw_subject_lv1 ON
junct_subject.subj_Lv1_ID = kw_subject_lv1.subjectLv1_ID INNER JOIN
dbo.kw_subject_lv2 ON junct_subject.subj_Lv2_ID =
kw_subject_lv2.subjectLv2_ID WHERE junct_subject.imageID = "+imgID ,
objConn);
objDA_subject.Fill(objDS_subject, "subjects");

dgSubjects.DataSource=objDS_subject;
dgSubjects.DataMember="subjects";
dgSubjects.DataBind();
}

</script>

<asp:DataGrid ID="dgSubjects" runat="server"
AutoGenerateColumns="false" ShowHeader="false">
<Columns>
<asp:BoundColumn DataField="subjectLv1_desc" />
<asp:BoundColumn DataField="subjectLv2_desc" />
</Columns>
</asp:DataGrid>

UserControl 2 - this is the one that's causing all the problems!!!
=============

<script runat="server" language="c#">

private string imgID;
private SqlConnection objConn = new SqlConnection("Server=SATURN;" +
"Database=Images_of_Empire;" + "User ID=Empire;Password=Empire");
private SqlDataAdapter objDA_subject;
private DataSet objDS_subject = new DataSet();

private SqlDataAdapter objDA_subjLv1;
private SqlDataAdapter objDA_subjLv2;

private DataView _subjectLv1List;
private DataView _subjectLv2List;

private void Page_Load()
{
if (!IsPostBack){
BindData();
}
}

public void BindData() {

imgID=Request.Cookies["ioe_imageID"].Value;

objDA_subject = new SqlDataAdapter("SELECT *,
kw_subject_lv1.subjectLv1_desc, kw_subject_lv2.subjectLv2_desc FROM
junct_subject INNER JOIN kw_subject_lv1 ON junct_subject.subj_Lv1_ID =
kw_subject_lv1.subjectLv1_ID INNER JOIN kw_subject_lv2 ON
junct_subject.subj_Lv2_ID = kw_subject_lv2.subjectLv2_ID WHERE
junct_subject.imageID = "+imgID , objConn);
objDA_subject.Fill(objDS_subject, "subjects");

dgSubjects.DataSource=objDS_subject;
dgSubjects.DataMember="subjects";
dgSubjects.DataBind();

objDA_subjLv1 = new SqlDataAdapter("SELECT * FROM kw_subject_lv1",
objConn);
objDA_subjLv1.Fill(objDS_subject, "subjectLv1");
_subjectLv1List = objDS_subject.Tables["subjectLv1"].DefaultView;

objDA_subjLv2 = new SqlDataAdapter("SELECT * FROM kw_subject_lv2",
objConn);
objDA_subjLv2.Fill(objDS_subject, "subjectLv2");
_subjectLv2List = objDS_subject.Tables["subjectLv2"].DefaultView;

//bindSelectBoxes();
}

private DataView getSubjectLv1(){
return _subjectLv1List;
}

private DataView getSubjectLv2(){
return _subjectLv2List;
}

private int getSelectedSubjectLv1(string _subjLv1){
for (int i=0; i<_subjectLv1List.Count; i++){
if(_subjectLv1List["subjectLv1_ID"].ToString() == _subjLv1){
return i;
}
}
return 0;
}

private int getSelectedSubjectLv2(string _subjLv2){
for (int i=0; i<_subjectLv2List.Count; i++){
if(_subjectLv2List["subjectLv2_ID"].ToString() == _subjLv2){
return i;
}
}
return 0;
}

private void dg_edit(Object s, DataGridCommandEventArgs e){
dgSubjects.EditItemIndex = e.Item.ItemIndex;
BindData();
}

private void dg_update(Object s, DataGridCommandEventArgs e){
SqlCommand objUpdateCmd = new SqlCommand("UPDATE junct_subject SET
subj_Lv1_ID=@level1ID, subj_Lv2_ID=@level2ID WHERE
imgSubjJUNCTid=@imgSubjJUNCT", objConn);

objUpdateCmd.Parameters.Add("@imgSubjJUNCT",((TextBox)e.Item.Cells[0].FindControl("imgSubjJunct_ID")).Text);
objUpdateCmd.Parameters.Add("@level1ID",
((DropDownList)e.Item.Cells[1].FindControl("ddl_subject1")).SelectedItem.Value);
objUpdateCmd.Parameters.Add("@level2ID",
((DropDownList)e.Item.Cells[2].FindControl("ddl_subject2")).SelectedItem.Value);

objConn.Open();
objUpdateCmd.ExecuteNonQuery();
objConn.Close();
dgSubjects.EditItemIndex = -1;
BindData();
}

private void dg_cancel(Object s, DataGridCommandEventArgs e){
dgSubjects.EditItemIndex = -1;
BindData();
}

private void refresh(Object s, EventArgs e){
BindData();
}

</script>

<asp:DataGrid ID="dgSubjects" runat="server"
AutoGenerateColumns="false" OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel" OnUpdateCommand="dg_update"
ShowHeader="false" EnableViewState="true">
<Columns>

<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "imgSubjJUNCTid") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="imgSubjJunct_ID" Text='<%#
DataBinder.Eval(Container.DataItem, "imgSubjJUNCTid") %>'
runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "subjectLv1_desc") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_subject1" runat="server" DataSource='<%#
getSubjectLv1() %>' SelectedIndex='<%#
getSelectedSubjectLv1(DataBinder.Eval(Container.DataItem,
"subjectLv1_ID").ToString()) %>' DataTextField="subjectLv1_desc"
DataValueField="subjectLv1_ID" />
</EditItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "subjectLv2_desc") %><br />
<asp:TextBox ID="tbTest1" Text=""/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_subject2" runat="server" DataSource='<%#
getSubjectLv2() %>' SelectedIndex='<%#
getSelectedSubjectLv2(DataBinder.Eval(Container.DataItem,
"subjectLv2_ID").ToString()) %>' DataTextField="subjectLv2_desc"
DataValueField="subjectLv2_ID" />
</EditItemTemplate>
</asp:TemplateColumn>

<asp:EditCommandColumn EditText="Edit" UpdateText="OK"
CancelText="Cancel"/>

</Columns>
</asp:DataGrid>

sorry to bombard you with so much code. thanks in advance for you
help. really is much appreciated!

Paul
 
S

Swanand Mokashi

Have you tried adding if (!IsPostBack) on Page_Load of the 1st web control ?


p.mc said:
hi again

thanks for your help, my page design is getting a bit complicated (to
be honest it's a bit of a hack!) but i've tried to cut out the
non-relevant bits.

basically i've got a main page with a datagrid, this contains a user
control with a second datagrid. the first data grid shows only one
record per page, while the user control shows all related records
(basically a master detail view). However i've actually got two user
controls, one in the <itemTemplate> and a slight variation in the
<editTemplateItem> which has a editcommand column.

displaying the main page loads the first (display only) user control
fine, but entering edit mode just gives a blank datagrid. If i remove
the if(!IsPostBack) condition from the binding on the user control,
data is displayed but is duplicated as above.

sorry if i've repeated myself there, but that helped to get it clearer
in my own head!

here's my code then!

main page
=========

<asp:DataGrid ID="dgImage" runat="server" AutoGenerateColumns="false"
BorderWidth="0px" BorderColor="#FFFFFF" OnEditCommand="dg_Edit"
OnCancelCommand="dg_Cancel" OnUpdateCommand="dg_Update"
EnableViewState="true" >
<columns>
<asp:TemplateColumn>
<ItemTemplate>
<table width="100%">
<tr>
<td class="dataCell">
<ioeKW:subjects id="dgSubject" runat="server" />
</td>
</tr>
</table>
</ItemTemplate>
<EditItemTemplate>
<table>
<tr>
<td colspan="3" class="dataCell">
<ioeKW:subjects_edit id="dgSubjectEdit" runat="server" />
</td>
</tr>
</table>
</EditItemTemplate>
</asp:TemplateColumn>
</columns>
</asp:Datagrid>

UserControl 1
=============

<script runat="server" language="c#">

private string imgID;
private SqlConnection objConn = new SqlConnection("Server=SATURN;" +
"Database=Images_of_Empire;" + "User ID=Empire;Password=Empire");
private SqlDataAdapter objDA_subject;
private DataSet objDS_subject = new DataSet();

private void Page_Load() {
BindData();
}

private void BindData() {
imgID=Request.Cookies["ioe_imageID"].Value;

objDA_subject = new SqlDataAdapter("SELECT *,
kw_subject_lv1.subjectLv1_desc, kw_subject_lv2.subjectLv2_desc FROM
junct_subject INNER JOIN dbo.kw_subject_lv1 ON
junct_subject.subj_Lv1_ID = kw_subject_lv1.subjectLv1_ID INNER JOIN
dbo.kw_subject_lv2 ON junct_subject.subj_Lv2_ID =
kw_subject_lv2.subjectLv2_ID WHERE junct_subject.imageID = "+imgID ,
objConn);
objDA_subject.Fill(objDS_subject, "subjects");

dgSubjects.DataSource=objDS_subject;
dgSubjects.DataMember="subjects";
dgSubjects.DataBind();
}

</script>

<asp:DataGrid ID="dgSubjects" runat="server"
AutoGenerateColumns="false" ShowHeader="false">
<Columns>
<asp:BoundColumn DataField="subjectLv1_desc" />
<asp:BoundColumn DataField="subjectLv2_desc" />
</Columns>
</asp:DataGrid>

UserControl 2 - this is the one that's causing all the problems!!!
=============

<script runat="server" language="c#">

private string imgID;
private SqlConnection objConn = new SqlConnection("Server=SATURN;" +
"Database=Images_of_Empire;" + "User ID=Empire;Password=Empire");
private SqlDataAdapter objDA_subject;
private DataSet objDS_subject = new DataSet();

private SqlDataAdapter objDA_subjLv1;
private SqlDataAdapter objDA_subjLv2;

private DataView _subjectLv1List;
private DataView _subjectLv2List;

private void Page_Load()
{
if (!IsPostBack){
BindData();
}
}

public void BindData() {

imgID=Request.Cookies["ioe_imageID"].Value;

objDA_subject = new SqlDataAdapter("SELECT *,
kw_subject_lv1.subjectLv1_desc, kw_subject_lv2.subjectLv2_desc FROM
junct_subject INNER JOIN kw_subject_lv1 ON junct_subject.subj_Lv1_ID =
kw_subject_lv1.subjectLv1_ID INNER JOIN kw_subject_lv2 ON
junct_subject.subj_Lv2_ID = kw_subject_lv2.subjectLv2_ID WHERE
junct_subject.imageID = "+imgID , objConn);
objDA_subject.Fill(objDS_subject, "subjects");

dgSubjects.DataSource=objDS_subject;
dgSubjects.DataMember="subjects";
dgSubjects.DataBind();

objDA_subjLv1 = new SqlDataAdapter("SELECT * FROM kw_subject_lv1",
objConn);
objDA_subjLv1.Fill(objDS_subject, "subjectLv1");
_subjectLv1List = objDS_subject.Tables["subjectLv1"].DefaultView;

objDA_subjLv2 = new SqlDataAdapter("SELECT * FROM kw_subject_lv2",
objConn);
objDA_subjLv2.Fill(objDS_subject, "subjectLv2");
_subjectLv2List = objDS_subject.Tables["subjectLv2"].DefaultView;

//bindSelectBoxes();
}

private DataView getSubjectLv1(){
return _subjectLv1List;
}

private DataView getSubjectLv2(){
return _subjectLv2List;
}

private int getSelectedSubjectLv1(string _subjLv1){
for (int i=0; i<_subjectLv1List.Count; i++){
if(_subjectLv1List["subjectLv1_ID"].ToString() == _subjLv1){
return i;
}
}
return 0;
}

private int getSelectedSubjectLv2(string _subjLv2){
for (int i=0; i<_subjectLv2List.Count; i++){
if(_subjectLv2List["subjectLv2_ID"].ToString() == _subjLv2){
return i;
}
}
return 0;
}

private void dg_edit(Object s, DataGridCommandEventArgs e){
dgSubjects.EditItemIndex = e.Item.ItemIndex;
BindData();
}

private void dg_update(Object s, DataGridCommandEventArgs e){
SqlCommand objUpdateCmd = new SqlCommand("UPDATE junct_subject SET
subj_Lv1_ID=@level1ID, subj_Lv2_ID=@level2ID WHERE
imgSubjJUNCTid=@imgSubjJUNCT", objConn);

objUpdateCmd.Parameters.Add("@imgSubjJUNCT",((TextBox)e.Item.Cells[0].FindControl("imgSubjJunct_ID")).Text);
objUpdateCmd.Parameters.Add("@level1ID",
((DropDownList)e.Item.Cells[1].FindControl("ddl_subject1")).SelectedItem.Value);
objUpdateCmd.Parameters.Add("@level2ID",
((DropDownList)e.Item.Cells[2].FindControl("ddl_subject2")).SelectedItem.Value);

objConn.Open();
objUpdateCmd.ExecuteNonQuery();
objConn.Close();
dgSubjects.EditItemIndex = -1;
BindData();
}

private void dg_cancel(Object s, DataGridCommandEventArgs e){
dgSubjects.EditItemIndex = -1;
BindData();
}

private void refresh(Object s, EventArgs e){
BindData();
}

</script>

<asp:DataGrid ID="dgSubjects" runat="server"
AutoGenerateColumns="false" OnEditCommand="dg_edit"
OnCancelCommand="dg_cancel" OnUpdateCommand="dg_update"
ShowHeader="false" EnableViewState="true">
<Columns>

<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "imgSubjJUNCTid") %>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox id="imgSubjJunct_ID" Text='<%#
DataBinder.Eval(Container.DataItem, "imgSubjJUNCTid") %>'
runat="server" />
</EditItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "subjectLv1_desc") %>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_subject1" runat="server" DataSource='<%#
getSubjectLv1() %>' SelectedIndex='<%#
getSelectedSubjectLv1(DataBinder.Eval(Container.DataItem,
"subjectLv1_ID").ToString()) %>' DataTextField="subjectLv1_desc"
DataValueField="subjectLv1_ID" />
</EditItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "subjectLv2_desc") %><br />
<asp:TextBox ID="tbTest1" Text=""/>
</ItemTemplate>
<EditItemTemplate>
<asp:DropDownList ID="ddl_subject2" runat="server" DataSource='<%#
getSubjectLv2() %>' SelectedIndex='<%#
getSelectedSubjectLv2(DataBinder.Eval(Container.DataItem,
"subjectLv2_ID").ToString()) %>' DataTextField="subjectLv2_desc"
DataValueField="subjectLv2_ID" />
</EditItemTemplate>
</asp:TemplateColumn>

<asp:EditCommandColumn EditText="Edit" UpdateText="OK"
CancelText="Cancel"/>

</Columns>
</asp:DataGrid>

sorry to bombard you with so much code. thanks in advance for you
help. really is much appreciated!

Paul
 
P

p.mc

Yeah, i've given that a go and it introduces the same problem as i'm
experiencing with the second form. It looks fine the first time the
page is loaded but then appears blank if i cancel or update from the
editing mode and the page reloads as a postback.

its interesting that removing the if condition in this case doesn't
result in the data being duplicated like i'm experiencing with the
second control

once again thanks for your continued help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top