Novice question about datalists

L

Logan McKinley

I have a datalist that takes two clicks to show the SelectedItem or
UpdateItem. The first click seems to fire the event but does not seem to
change the page, the second click doesn't fire the event but changes the
page. I have noticed people don't bind the data on post back but when i
used if(!Page.IsPostBack) to only bind the date on the first load it seems
to loose everything but the date of things that have been displayed with
SelectedItem or UpdateItem.
=========== My Code (.aspx)===========
<table class="InnerTable" cellSpacing="0" cellPadding="0" border="0">
<tr>
<td class="Table-Header-Cell">Notes</td>
<td class="Table-Empty-Cell">&nbsp;</td>
</tr>
<tr>
<td class="Table-Body-Cell" colSpan="2">
<asp:datalist id="dlNotes" OnEditCommand="dlNotes_EditCommand"
Runat="server">
<SelectedItemTemplate>
<DIV class="Table-Item">
<TABLE class="Table-Item" cellPadding="0" width="99%" border="0">
<TR class="Table-Item-Top">
<TD align="center" width="45%"><%#
DataBinder.Eval(Container.DataItem,
"FirstName")%>&nbsp;<%#DataBinder.Eval(Container.DataItem,
"LastName")%></TD>
<TD
align="center">&nbsp;&nbsp;<%#DataBinder.Eval(Container.DataItem,
"Date")%></TD>
</TR>
<TR>
<TD class="StandOut" align="center" width="45%">via:
</TD>
<TD align="center"><%# DataBinder.Eval(Container.DataItem,
"MeetingMethod")%></TD>
</TR>
<TR>
<TD colSpan="2"><%# DataBinder.Eval(Container.DataItem,
"Notes")%></TD>
</TR>
<TR>
<TD class="StandOut" align="center" width="45%">Outcome:</TD>
<TD align="center"><%# DataBinder.Eval(Container.DataItem,
"MeetingOutcome")%></TD>
</TR>
<TR class="Table-Item-Bottom">
<TD colSpan="2">[<A href="#">Shrink</A> |
<asp:LinkButton id="lbNoteUpdate" Runat="server"
CommandName="Edit"> Update </asp:LinkButton>|
<A href="#">Delete</A>]</TD>
</TR>
</TABLE>
</DIV>
</SelectedItemTemplate>
<ItemTemplate>
<%# DataBinder.Eval(Container.DataItem, "Date")%>
&nbsp;&nbsp;&nbsp;
<asp:LinkButton id="Linkbutton5" onmouseover="popup('this is the
note','blue');" onmouseout="kill();"
Runat="server">
<%#ShortenString(DataBinder.Eval(Container.DataItem,
"Notes").ToString(), 30, "...", "Empty Note")%>
</asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<DIV class="Table-Item">
<TABLE class="Table-Item" cellPadding="0" width="99%" border="0">
<TR class="Table-Item-Top">
<TD align="center" width="45%"><%#
DataBinder.Eval(Container.DataItem,
"FirstName")%>&nbsp;<%#DataBinder.Eval(Container.DataItem,
"LastName")%></TD>
<TD
align="center">&nbsp;&nbsp;<%#DataBinder.Eval(Container.DataItem,
"Date")%></TD>
</TR>
<TR>
<TD class="StandOut" align="center" width="45%">via:
</TD>
<TD align="center">
<asp:DropDownList id="cmMeetingMethod"
runat="server"></asp:DropDownList></TD>
</TR>
<TR>
<TD colSpan="2">
<asp:TextBox id="txtNote" runat="server" Height="43px"
TextMode="MultiLine" Width="393px">111111111111222</asp:TextBox></TD>
</TR>
<TR>
<TD class="StandOut" align="center" width="45%">Outcome:</TD>
<TD align="center">
<asp:DropDownList id="cbMeetingOutcome"
runat="server"></asp:DropDownList></TD>
</TR>
<TR>
<TD class="StandOut" align="center" width="45%">Restricted:</TD>
<TD align="center">
<asp:DropDownList id="cbRestricted">
</asp:DropDownList></TD>
</TR>
<TR class="Table-Item-Bottom">
<TD colSpan="2">[<A href="#">Shrink</A> |
<asp:LinkButton id="Linkbutton6" Runat="server"
CommandArgument="Edit"> Update </asp:LinkButton><A href="#">Delete</A>]</TD>
</TR>
</TABLE>
</DIV>
</EditItemTemplate>
</asp:datalist></td>
</tr>
<tr>
<td class="Table-Bottom-Cell" colSpan="2">[<A href="#">Shrink All</A> | <A
href="#">Expand
All</A> | <A href="#">Add new note</A>]
</td>
</tr>
</table>
=========== My Code (.cs)===========
public class UserDetails : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataList dlNotes;
public string UserInfo;

private void Page_Load(object sender, System.EventArgs e)
{
string SQL;
Data DObject = new Data(); // this is my own class that seems to work
correctly
SQL = "SELECT ContactMeet.Date, ContactMeet.MeetingMethod,
ContactMeet.Notes, ContactMeet.MeetingOutcome, Contacts.FirstName,
Contacts.LastName "
+ "FROM Contacts INNER JOIN ContactMeet ON Contacts.ContactID =
ContactMeet.ContactedBy ";
System.Data.DataSet dsNotes = Data.GetDataSetNow(SQL);
dlNotes.DataSource = dsNotes;
dlNotes.DataBind();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dlNotes.ItemCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlNotes_ItemComma
nd);
this.dlNotes.EditCommand += new
System.Web.UI.WebControls.DataListCommandEventHandler(this.dlNotes_EditComma
nd);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

public void dlNotes_EditCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlNotes.EditItemIndex=e.Item.ItemIndex;

}

private void dlNotes_ItemCommand(object source,
System.Web.UI.WebControls.DataListCommandEventArgs e)
{
dlNotes.SelectedIndex = e.Item.ItemIndex;

}
public string ShortenString(string s, int len, string trailing, string
emptyResponse)
{
if(s.Length > len)
s = s.Substring(0,len-trailing.Length)+trailing;
else if(s.Length == 0)
s = emptyResponse;
return s;
}

}
======================================


if anyone knows of a good tutorial or sample code that dimenstrates how to
use a datalist to display, update, add and delete items that would be
wonderful.

Thanks in advance,
~Logan
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top