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
ataGrid 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
atagrid>
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
ataGrid ID="dgSubjects" runat="server"
AutoGenerateColumns="false" ShowHeader="false">
<Columns>
<asp:BoundColumn DataField="subjectLv1_desc" />
<asp:BoundColumn DataField="subjectLv2_desc" />
</Columns>
</asp
ataGrid>
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>
<aspataGrid 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>
<aspropDownList 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>
<aspropDownList 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>
</aspataGrid>
sorry to bombard you with so much code. thanks in advance for you
help. really is much appreciated!
Paul