deleting items checked on page 2 in datagrid

G

gh0st54

Hi

i'm working on deleting items in a datagrid selected using a checkbox
used to work fine

the problem is this if i delete something on page 2 it's the item on
page 1 that get deleted.

i have tried not to bind in the page load (should not bind there
because it's executed prior to delete function)

i have tried with viewstate enable nothing happened

i do rebind after deletion

i thought that by keeping the view state it would reload the grid on
the right page then the delete function would do it's job on the right
item.

I have seen that on the second page my hidden field has no value , how
come ? I have rebinded after changing the page index

any help will be appreciated

here is the code :
--------
aspx:
-------
<asp:datagrid id="dgMessages" runat="server" Width="100%"
BorderWidth="0px" CellPadding="0" AutoGenerateColumns="False"
AllowPaging="True">
<AlternatingItemStyle HorizontalAlign="Left"
VerticalAlign="Middle" BackColor="White"></AlternatingItemStyle>
<ItemStyle BackColor="#E1F0FF"></ItemStyle>
<Columns>
<asp:TemplateColumn>
<HeaderStyle Width="5pc"
CssClass="dgHeader"></HeaderStyle>
<ItemTemplate>
<CENTER>
<asp:Image id=Image1 runat="server" ImageUrl='<%#
DataBinder.Eval(Container.DataItem,"ImagePath")!=null?DataBinder.Eval(Container.DataItem,"ImagePath"):""
%>'>
</asp:Image></CENTER>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False"
HeaderText="MessageId">
<ItemTemplate>
<asp:TextBox id=txtMsgId runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"MessageId") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Member Name">
<HeaderStyle Width="10pc"
CssClass="dgHeader"></HeaderStyle>
<ItemTemplate>
<asp:LinkButton CssClass="gridbluetext"
id="LinkButton1" runat="server" CommandArgument='<%#
DataBinder.Eval(Container.DataItem,"MessageId") %>'>
<%# DataBinder.Eval(Container.DataItem,"UserName") %>
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="Date/Time GMT">
<HeaderStyle Width="30pc"
CssClass="dgHeader"></HeaderStyle>
<ItemTemplate>
<asp:Label id="lblDate" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"Date") %>'
CssClass="gridbluetext">
</asp:Label>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="Subject"
HeaderText="Subject">
<HeaderStyle Width="50pc"
CssClass="dgHeader"></HeaderStyle>
<ItemStyle CssClass="gridbluetext"></ItemStyle>
</asp:BoundColumn>
<asp:TemplateColumn>
<HeaderStyle Width="5pc"
CssClass="dgHeader"></HeaderStyle>
<ItemStyle CssClass="gridbluetext"></ItemStyle>
<ItemTemplate>
<CENTER>
<asp:CheckBox id="chkDelete"
runat="server"></asp:CheckBox></CENTER>
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn Visible="False" HeaderText="Online">
<ItemTemplate>
<asp:TextBox id="Online" runat="server" Text='<%#
DataBinder.Eval(Container.DataItem,"Online") %>'>
</asp:TextBox>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Right"
Mode="NumericPages"></PagerStyle>
</asp:datagrid>
--------
aspx.cs:
--------

private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
this.UpdateGrid(this.CreateData());
}

}

private void btnDelete_Click(object sender,
System.Web.UI.ImageClickEventArgs e) {
wc.CheckBox chkSelected;
wc.TextBox txtMsg;
foreach( wc.DataGridItem dgItem in dgMessages.Items ){
chkSelected = (wc.CheckBox)dgItem.FindControl("chkDelete");
if (chkSelected.Checked){
txtMsg = (wc.TextBox)dgItem.FindControl("txtMsgId");
if (txtMsg.Text.Length > 0){
this.DeleteMessage(txtMsg.Text); //calling stored proc to delete
}
}
}
this.UpdateGrid(this.CreateData());

}
private DataSet CreateData(){
this.cmdGetMessagesHeaders.Parameters["@MemberId"].Value= new
Guid(Session["MemberId"].ToString());//MemberId;
this.cmdGetMessagesHeaders.Parameters["@Direction"].Value= 0;
this.cmdGetMessagesHeaders.Parameters["@Error"].Value= null;

daData = new SqlDataAdapter();
daData.SelectCommand = this.cmdGetMessagesHeaders ;

DataSet ds = new DataSet();

try {
this.cnn.Open();
daData.Fill(ds);
}
catch(System.Exception ex) {
Response.Write(ex.Message);
}
finally {
this.cnn.Close();
}
return ds;
}
private void UpdateGrid(DataSet ds){
if (ds.Tables[0].Columns.Count > 1){
this.dgMessages.DataSource= ds.Tables[0];
this.dgMessages.DataBind();
this.dgMessages.Visible=true;
this.lblNoMessages.Visible=false;
this.dgMessages.Enabled=true;
}
else {
this.dgMessages.Visible=false;
this.dgMessages.Enabled=false;
this.lblNoMessages.Visible=true;
}
}

private void dgMessages_ItemDataBound(object sender,
System.Web.UI.WebControls.DataGridItemEventArgs e) {
if((e.Item.ItemType == wc.ListItemType.Item) ||
(e.Item.ItemType == wc.ListItemType.AlternatingItem)){

wc.LinkButton lnkBtn;
wc.TextBox txtOnline;
wc.Label lblDate;

lnkBtn = (wc.LinkButton)e.Item.FindControl("LinkButton1");
txtOnline = (wc.TextBox)e.Item.FindControl("Online");
lblDate = (wc.Label)e.Item.FindControl("lblDate");

lblDate.Text = functions.ShowDateTime(lblDate.Text.ToString());

lnkBtn.Command +=new
System.Web.UI.WebControls.CommandEventHandler(lnkBtn_Command);

if (txtOnline.Text =="True"){
lnkBtn.CssClass ="orangetext";
}
else {
lnkBtn.CssClass ="bluetext";
}
}
}

private void dgMessages_PageIndexChanged(object source,
System.Web.UI.WebControls.DataGridPageChangedEventArgs e) {
this.dgMessages.CurrentPageIndex = e.NewPageIndex;
this.dgMessages.DataSource=this.CreateData();
this.dgMessages.DataBind();
}
 

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,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top