Asp:Repeater Datasouce - Datatable Checkbox inside Repeater. Delete the current row - asp.net

Couldn't find anything like it.
The page has a Repeater, the data source is a datatable. Rows from the page are added to the datatable, then Repeater is updated and displayed on the page. For example, a user has added incorrect information and needs to remove something from Repeater by placing a check mark in the Checkbox. If the Checkbox is checked, the selected data (a row in the datatable ) should be removed from the data source and Repeater should be updated. How can this be implemented and whether it is possible at all? My code:
<asp:UpdatePanel runat="server" ID="UpdatePanel1" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:TableCell Width="80px" BackColor="#e3a99a" HorizontalAlign="Right">
<asp:CheckBox ID="cb_GetOut" runat="server" AutoPostBack="true" Checked="false" Text="DeleteRow" style="padding-right:5px" />
</asp:TableCell>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Page.IsPostBack = False Then
dtTest_add.Columns.AddRange(New DataColumn(3) {New DataColumn("text1"), New DataColumn("text2"), New DataColumn("text3"), New DataColumn("text4")})
ViewState("dtTest_add") = dtTest_add
Else
dtTest_add = ViewState("dtTest_add")
Repeater1.DataSource = TryCast(ViewState("dtTest_add"), DataTable)
Repeater1.DataBind()
End If
End Sub
Protected Sub btn_addTest_Click(sender As Object, e As System.EventArgs) Handles btn_addTest.Click
dtTest_add.Rows.Add(txt1.Text, txt2.Text, txt3.Text, txt4.Text)
dtTest_add = ViewState("dtTest_add")
ViewState("dtTest_add") = dtTest_add
Repeater1.DataSource = TryCast(ViewState("dtTest_add"), DataTable)
Repeater1.DataBind()
UpdatePanel1.Update()
End Sub
Protected Sub Repeater1_ItemCreated(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemCreated
Dim cb_GO As CheckBox = CType(e.Item.FindControl("cb_GetOut"), CheckBox)
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(cb_GO)
End Sub
Protected Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Dim cb_GO As CheckBox = DirectCast(e.Item.FindControl("cb_GetOut"), CheckBox)
cb_GO.Attributes("onclick") = "this.checked = (" + dtTest_add + ").deleteRow(" + CStr(e.Item.ItemIndex) + ");"
End Sub
The "btn_addTest" button is on a form not in Repeater. With it I add data to the data source. Then update the Repeater.
I handle the "onclick" Checkbox event in this line of code
cb_GO.Attributes("onclick") = "this.checked = (" + dtTest_add +
").deleteRow(" + CStr(e.Item.ItemIndex) + ");"
It is necessary to remove data from datatable, and Repeater will simply be updated then and all. But how do you do that? Can't get through to a datatable with the Repeater in ItemDataBound.

Related

vb.net TextBox submit not changing

I've got a legacy VB.Net app that I'm modifying in VS 2010; here's the relevant code:
<form id="form1" runat="server">
<asp:GridView ID="gvCommentsEdit" runat="server" ShowHeader="False">
<Columns>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
*** On
<asp:Label ID="lblTimestamp" runat="server" Text='<%# Bind("comment_date") %>'></asp:Label>
<asp:Label ID="lblUpdateBy" runat="server" Text='<%# Bind("update_by") %>'></asp:Label> commented:<br />
<asp:TextBox ID="txtEditCommentPopup" runat="server" Columns="55" Rows="10" Text='<%# bind("text") %>' TextMode="MultiLine"></asp:TextBox>
<asp:LinkButton ID="lnkSaveComment" runat="server" OnClick="lnkSaveComment_Click">Save</asp:LinkButton>
<asp:LinkButton ID="lnkCancelEdit" runat="server" OnClick="lnkCancelEdit_Click">Cancel</asp:LinkButton>
<asp:HiddenField ID="hdnCommentID" Value='<%# Bind("comment_id") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
Code Behind is:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim IdNumber As Integer = sender.ClientQueryString
Dim mydata As New Profile_Data
gvCommentsEdit.DataSource = mydata.returnCommentsById(RequestedUsername, IdNumber)
gvCommentsEdit.DataBind()
End Sub
Sub lnkSaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myRow = sender.parent
Dim mydata As New Profile_Data
Dim IdNumber As String = CType(myRow.FindControl("hdnCommentID"), HiddenField).Value
Dim text As String = CType(myRow.FindControl("txtEditCommentPopup"), TextBox).Text
mydata.UpdateComment(IdNumber, text)
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "CloseWindowScript", "window.opener.location.href = window.opener.location;window.close();", True)
End Sub
End Class
This page is a popup from the main page and populates fine. The problem is when I change the txtEditCommentPopup TextBox, which then calls lnkSaveComment_Click(), it has the original textbox values, not the changed value. I'm not using AutoPostBack. Any ideas on why? Thanks!
Assuming the database update is working correctly, the problem is that you are binding the GridView on every PostBack, but not after the data is updated.
When you click your button, you should see that your Page_Load method is called, your GridView is bound, then your lnkSaveComment_Click method is called, updating the database. But afterwards, you are not rebinding with the new data.
So my guess is that if you were to click the update button again (or cause another PostBack somehow), your new data would show up.
The fix would be to make sure you bind your GridView again after you update the data.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridView()
End If
End Sub
Sub lnkSaveComment_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim myRow = sender.parent
Dim mydata As New Profile_Data
Dim IdNumber As String = CType(myRow.FindControl("hdnCommentID"), HiddenField).Value
Dim text As String = CType(myRow.FindControl("txtEditCommentPopup"), TextBox).Text
mydata.UpdateComment(IdNumber, text)
BindGridView()
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "CloseWindowScript", "window.opener.location.href = window.opener.location;window.close();", True)
End Sub
Sub BindGridView()
Dim IdNumber As Integer = Page.ClientQueryString
Dim mydata As New Profile_Data
gvCommentsEdit.DataSource = mydata.returnCommentsById(RequestedUsername, IdNumber)
gvCommentsEdit.DataBind()
End Sub
I added this in your page load
If Not IsPostBack Then
'your code
End If
so replace your Page_Load with this:-
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim IdNumber As Integer = sender.ClientQueryString
Dim mydata As New Profile_Data
gvCommentsEdit.DataSource = mydata.returnCommentsById(RequestedUsername, IdNumber)
gvCommentsEdit.DataBind()
End If
End Sub
Hope this will help you.

ItemCommand on nested Repeater

I have a simple aspx page which contains 2 nested repeaters. Each of these have buttons on them. When the user clicks on a button on the outer repeater, I can capture the ItemCommand in my codebehind, however when the user clicks on an inner one, I cannot capture it.
I have read in other threads about needing to attach the event manually to the inner repeater, but cannot work this out.
Can anyone help?
Here is the aspx. I'm using vb.net code behind
<asp:Repeater runat="server" ID="ParentRepeater">
<ItemTemplate>
<li id="P<%#DataBinder.Eval(Container, "DataItem.id")%>">
<%#DataBinder.Eval(Container, "DataItem.name")%>
<asp:Button runat="server" ID="adedit" Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>'
class="pages-edit" />
<asp:Button runat="server" ID="addel" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
class="pages-delete" />
<ul class="page-section sub innerdrag">
<asp:Repeater runat="server" ID="childrepeater">
<ItemTemplate>
<li id="<%#DataBinder.Eval(Container, "DataItem.id")%>,">
<%#DataBinder.Eval(Container, "DataItem.name")%><asp:Button runat="server" ID="ad_edit"
Text="Edit" CommandName='<%#DataBinder.Eval(Container, "DataItem.id")%>' class="pages-edit" />
<asp:Button runat="server" ID="ad_del" Text="Delete" CommandName='<%#DataBinder.Eval(Container, "DataItem.xid")%>'
class="pages-delete" />
</li>
</ItemTemplate>
</asp:Repeater>
</ul>
</li>
</ItemTemplate>
</asp:Repeater>
Still not firing so I've put the updated VB here too
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim DBFunctions As New DBFunctions.Functions
Dim dstmp As New DataSet
Dim dstmp2 As New DataSet
dstmp = DBFunctions.SQLDataSet("SELECT id,name, 'x'+cast(id as varchar(50)) as xid from pages where parent = 0 and coalesce(active,1)=1 order by orderby asc", "data")
dstmp2 = DBFunctions.SQLDataSet("SELECT id,name , 'x'+cast(id as varchar(50)) as xid,parent from pages where parent >0 and coalesce(active,1)=1 order by orderby asc", "data2")
Dim allData As New DataSet
allData.Tables.Add(dstmp.Tables(0).Copy)
allData.Tables.Add(dstmp2.Tables(0).Copy)
allData.Relations.Add(New DataRelation("Children", allData.Tables(0).Columns("ID"), allData.Tables(1).Columns("parent")))
ParentRepeater.DataSource = allData
ParentRepeater.DataBind()
sdhfunctions.Close()
End If
End Sub
Protected Sub repMenu1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles ParentRepeater.ItemDataBound
Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If dv IsNot Nothing Then
Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
If repSubMenu IsNot Nothing Then
AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
repSubMenu.DataSource = dv.CreateChildView("Children")
repSubMenu.DataBind()
End If
End If
End Sub
Protected Sub ParentRepeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles ParentRepeater.ItemCreated
Dim dv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
If dv IsNot Nothing Then
Dim repSubMenu As Repeater = TryCast(e.Item.FindControl("childrepeater"), Repeater)
If repSubMenu IsNot Nothing Then
AddHandler repSubMenu.ItemCommand, AddressOf childrepeater_ItemCommand
repSubMenu.DataSource = dv.CreateChildView("Children")
repSubMenu.DataBind()
End If
End If
End Sub
Protected Sub childrepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
Dim stophere As String = ""
End Sub
I put a breakpoint on stophere, and it never gets hit.
Sorry guys, I'm really confused :(
Try this:
Protected Sub ParentRepeater_ItemCreated(ByVal sender As Object, ByVal e As RepeaterItemEventArgs) Handles ParentRepeater.ItemCreated
Dim childRepeater As Repeater = DirectCast(e.Item.FindControl("childrepeater"), Repeater)
AddHandler childRepeater.ItemCommand, AddressOf childrepeater_ItemCommand
childRepeater.DataSource = "the data source for childRepeater"
childRepeater.DataBind()
End Sub
Protected Sub childrepeater_ItemCommand(ByVal source As Object, ByVal e As RepeaterCommandEventArgs)
End Sub

How to get controls ID's on Pageload

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lbl_Date As New Label
Dim i as int16
i = 20
While (i < i + 1)
lbl_Date = TryCast(Page.FindControl("lbl_Date" + i.ToString), Label)
lbl_Date = "Value"
i = i + 1
End While
End Sub
This is returning Nothing in lbl_Date variable. If i don't use this in Page_Load, it works just fine. Only in Page_Load it doesn't work. Any Explanation :::: Well what i am trying to do here is . i have 100 label name are lable1 to lable100. I can do like label1 =Value and label2 =Value and so on but i am trying to put in loop so i have less code. just for the Info... This code work but not in Page_Load , I am thinking every one looking to the code but this has to do with page life cycle
<asp:content runat="server" id="leftcontent" contentplaceholderid="LeftSidePanel" xmlns:asp="#unknown">
<div id="mydiv" runat="server">
<asp:linkbutton id="Lnk_Heading1" runat="server"> </asp:linkbutton>
<asp:label id="lbl_Date1" runat="server"> </asp:label>
<asp:label id="lbl_Detail1" runat="server"> </asp:label>
<asp:button id="Button1" runat="server" text="Button" />
</div>
</asp:content>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim lbl_Date As New Label
Dim i as int16 i = 20
While (i < i + 1) lbl_Date = TryCast(Mydiv.FindControl("lbl_Date" + i.ToString), Label)
lbl_Date = "Value"
i = i + 1
End While
End Sub
Problem was , I didn't Used DIV in my aspx page . When i put my code in div and use the reference of the div in codebehind then it work.

can't find a control inside my gridview

I have a simple gridview that contains a label in one of the rows. I'm trying to access that label in the RowDataBound event, but for some reason I keep getting a "Object reference not set to an instance of an object." error on the line where I am using FindControl.
I've tried using "gvQReport.FindControl", "e.Row.FindControl", and "Me.FindControl" but nothing works.
Am I not doing this correctly?
Thanks!
Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
Dim lblTest As Label = CType(gvQReport.FindControl("lblTest"), Label)
lblTest.Text = "test Label"
End Sub
<asp:GridView ID="gvQReport" OnRowDataBound="gvQReport_RowDataBound" runat="server">
<Columns>
<asp:TemplateField HeaderText="Test">
<ItemTemplate>
<asp:Label ID="lblTest" runat="server" Text=""></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
The Row property of GridViewRowEventArgs is the current row, look for your control there instead of the whole GridView.
Protected Sub gvQReport_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim lblTest As Label = CType(e.Row.FindControl("lblTest"), Label)
lblTest.Text = "test Label"
End If
End Sub

how to get value of dropdownlist which is inside a gridview on the click of a button?

I have a dropdownlist inside the gridview. Now i want when i click on a button then i can check the value of dropdownlist. I have fired rowcommand event of gridview for it but debugger is not able to go reach there..Please help me..My Code is
Protected Sub grd_Test_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd_Test.RowCommand
If e.CommandName = "Select" Then
End If
End Sub
my Source code is
<asp:GridView ID="grd_UnAssignProperties" runat="server" AutoGenerateColumns="False"><Columns>
<asp:TemplateField HeaderText="Assign To">
<ItemTemplate>
<asp:DropDownList ID="drp_UnAssignProp" runat="server">
<asp:ListItem Value="" >Default</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns></asp:GridView><tr><td><asp:Button ID="btn_Save" runat="server" CommandName="Select" Text="Submit" />
try this
DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drp_UnAssignProp");
string val = ddl.SelectedValue;
try
string val = (DropDownList)GridView1.Rows[e.RowIndex].Cells[0]
.FindControl("drp_UnAssignProp").SelectedValue;
First of all, since the button btn_Save isn't inside the GridView, clicking on it won't raise the grd_Test_RowCommand, either move the button inside GridView or you have to raise it manually like this:
Copied from asp.net forum:
Protected Sub Button1_Click(sender As Object, e As EventArgs)
Dim commandArgs As New CommandEventArgs("Command Name Here", "Your Command Argument Here")
'You can pass any row
'You can also skip the row parameter and get the row from Command Argument
Dim eventArgs As New GridViewCommandEventArgs(GridView1.Rows(0), GridView1, commandArgs)
GridView1_RowCommand(GridView1, eventArgs)
End Sub
Now regarding your original question, this is how you can get the selected value of DropDownList inside RowCommand event:
Edit: Fixed code to get the current row, for which the RowCommand event was raised
Protected Sub grd_Test_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles grd_Test.RowCommand
If e.CommandName = "Select" Then
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim ddl as DropDownList = CType(row.FindControl("drp_UnAssignProp", DropDownList)
Dim selectedValue as String = ddl.Text
End If
End Sub

Resources