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 6 5705
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.mc" <pa*************@googlemail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com... 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
I've tried explictly setting enableViewState to true but that's not
helped,
thanks for your help, any other posibilities you can think of?
can you post some code? that will help to see what is wrong
"p.mc" <pa*************@googlemail.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com... I've tried explictly setting enableViewState to true but that's not helped,
thanks for your help, any other posibilities you can think of?
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[i]["subjectLv1_ID"].ToString() == _subjLv1){
return i;
}
}
return 0;
}
private int getSelectedSubjectLv2(string _subjLv2){
for (int i=0; i<_subjectLv2List.Count; i++){
if(_subjectLv2List[i]["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",((Text Box)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.Da taItem,
"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.Da taItem,
"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
Have you tried adding if (!IsPostBack) on Page_Load of the 1st web control ?
"p.mc" <pa*************@googlemail.com> wrote in message
news:11**********************@z34g2000cwc.googlegr oups.com... 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[i]["subjectLv1_ID"].ToString() == _subjLv1){ return i; } } return 0; }
private int getSelectedSubjectLv2(string _subjLv2){ for (int i=0; i<_subjectLv2List.Count; i++){ if(_subjectLv2List[i]["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",((Text Box)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.Da taItem, "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.Da taItem, "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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Ann Morris |
last post by:
INTRODUCTION
One of the most powerful aspects of .NET and Windows Forms is data
binding. Data binding is the process of associating user interface
(UI) elements with a data source to generate a...
|
by: vinayak |
last post by:
Hi
I am displaying data in Datagrid in ASP.NET with
Edit/Update functionality for each row. On the same page I
have 2 Button controls which submits the request to
server. These button controls...
|
by: Anonymous |
last post by:
Hello,
I am loading a usercontrol with the LoadControl method
and need to pass some data to the property of that
control. How can I do that?
THanks
|
by: hope |
last post by:
Hi,
How can I format a string field using Data Formatting Expression property in
datagrid?
For example: format last name from BROWN to Brown.
Thanks
|
by: Tejpal Garhwal |
last post by:
I have datagrid filled with some data rows. At the run time i want know how
many total rows are there in the data grid ?
Any idea ? Any Suggestions ?
Thanks in advance
Tej
|
by: emzyme20 |
last post by:
Hi,
I am trying to populate a list control that is bound to a data table
but the display member needs to come from a different data table.
I have two list controls in C#, one displaying...
|
by: Suresh |
last post by:
Using ASP.NET 1.1
----------------------
I have a custom collection of object that I'm binding to a datagrid. Each
of these objects have another collection inside them. I have a user control...
|
by: Anil Gupte |
last post by:
After reading a tutorial and fiddling, I finally got this to work. I can
now put two tables created with a DataTable class into a DataRelation.
Phew! And it works!
Dim tblSliceInfo As New...
|
by: Stephen Barrett |
last post by:
I have an application that was originally built with ASP.Net 1.1.
We finally got permission to migrate to 2.0. Due to time constraints we
migrated the web projects to 2.0 web application...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |