Ok I am using a column with check box to be able to select my data row from a GridView. But The OnCheckChanged event won't fire. I have tried reading articles to make it work and copy code exactly and it just won't fire. I am using vb.net and asp.net
<asp:GridView ID="locationDetailGrid" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate >
<asp:CheckBox ID="locationSelection" AutoPostBack="true"
runat="server" OnCheckedChanged="CheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Protected Sub CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim checkbox As CheckBox = DirectCast(sender, CheckBox)
Dim row As GridViewRow = DirectCast(checkbox.NamingContainer, GridViewRow)
Response.Write(row.Cells(0).Text)
End Sub
Probably because you're databinding the GridView also on postbacks. Add an If Not Page.IsPostback into Page_Load around your databinding stuff of the GridView.
If you rebind the GridView on postbacks, you're preventing events from triggering.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGridToDataSourceAndDataBind()
End If
End Sub
Related
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.
I've placed some controls in a panels. When the page postback, I'm trying to retrieve the posted values, but only the older value seems to be there.
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
Dim _txtFName As TextBox = FindControl("editNamePanel").FindControl("txtFName")
Dim _txtMName As TextBox = FindControl("editNamePanel").FindControl("txtMName")
Dim _txtLName As TextBox = FindControl("editNamePanel").FindControl("txtLName")
End Sub
Even when I hover over the e EventArgs is null. Am I missing something?
EDIT
I'm getting new values when I put the above code in the page load event handler
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
Handles Me.Load
If Page.IsPostBack Then
'The above code here ...
End If
End Sub
Thank for helping
In your aspx
<asp:Panel ID="editNamePanel" runat="server">
<asp:TextBox ID="txtFName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtMName" runat="server"></asp:TextBox>
<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" />
</asp:Panel>
In your code behind
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Dim _txtFName As TextBox = txtFName
Dim _txtMName As TextBox = txtMName
Dim _txtLName As TextBox = txtLName
End Sub
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
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
I have added the AutoPostBack = "true" attribute to the drop down list.
It doesn't work.
<asp:DropDownList CssClass="dropDownList" ID="ddlBusinessUnit"
AutoPostBack="true" runat="server" Width="250px"
OnSelectedIndexChanged="ddlBusinessUnit_SelectedIndexChanged">
</asp:DropDownList>
code behind:
Protected Sub ddlBusinessUnit_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
Response.Write("Hello")
End
How can I ensure that the event method is called?
Are you populating your Dropdownlist on every Postback?
Use IsPostBack to check whether the page is loaded for the first time or not.
If Not IsPostBack Then
BinDdlBusinessUnit()
End If