In my project, I have a datarepeater, in an updatepanel, with a linkbutton. When the user clicks the linkbutton, a partial post back occurs and changes the text property of the linkbutton. The problem is: when the user clicks the same button again, even though i can physically see the new text value on the web page, when the debugger hits the event, the sender object says the text value is the old value, not the last updated. Anyone know why? Here is the code:
<asp:LinkButton runat="server" ID="lbEdit" Text="Edit" EnableViewState="true" OnClick="edit_click" CommandArgument='<%# Eval("user.networkId") %>'></asp:LinkButton>
codebehind:
Protected Sub edit_click(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As LinkButton = CType(sender, LinkButton)
Dim userId As String = btn.CommandArgument
If (btn.Text = "Edit") Then
btn.Text = "Save"
else
btn.Text = "Edit"
end if
end sub
Most likely you are rebinding your data on postback, so it is resetting your value. Then the button click event is firing, so yes you are setting Text correctly. But when you postback again, the text is getting reset.
Wrap your DataBind event in a If Not IsPostBack:
If Not IsPostBack
'Bind your datasource
End If
Related
I have a datagrid that is populated through user input, and in that grid I have a checkbox that should remove the row from the datagrid if checked. I have set the EnableViewState to true and false with no results. It appears that if the checkbox is clicked, the event does not fire. Debugging never gets to the event. Can anyone point me? Here's the markup:
<asp:TemplateColumn HeaderText="Remove">
<ItemTemplate>
<asp:checkbox ID="chkSelection" runat="server" AutoPostBack="True" Checked="false" OnCheckChanged="EPACheck_Clicked"/>
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="50px" />
And here's the code behind for the event:
Protected Sub EPACheck_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Dim ckbx As CheckBox = CType(sender, CheckBox)
Dim dgItem As DataGridItem = CType(ckbx.NamingContainer, DataGridItem)
Dim rcDelete As String = dgItem.ItemIndex
CType(Session("mytable"), DataTable).Rows.RemoveAt(Convert.ToInt32(rcDelete))
BindgrdEPA()
End Sub
Your method doesn't handle the event. Try the below snippet.
Protected Sub EPACheck_Clicked(ByVal sender As Object, ByVal e As EventArgs)
Handles chkSelection.CheckedChanged
The reason OnCheckedChanged wasn't firing for me was because I disabled IE8's Protected Mode.
Once I enabled Protected Mode OnCheckedChanged worked.
Previously, I had disabled Protected Mode to test a cookie problem with one of our web applications. I didn't re-enable Protected Mode when I finished that task.
To re-enable Protected Mode I did this:
Clicked on Tools\Internet Options.
Clicked on Security tab.
Clicked on Reset all zones to default level.
I suppose I could have clicked "Enable Protected Mode" checkbox.
I asked this on the telerik forums, but sometimes responses can be slow there. I was wondering if anyone here know how to go about this.
I am in a situation where a user will have a variable number of items and beside each item I want a RadNumericTextBox. I was thinking of using the RadListView and setting the template to be the item name and a RadNumericTextBox associated with it. I want to ignore the edit, create, and the more advanced features of the RadListView. I just want a list of items with the input boxes that will auto post back when a user has changed the value.
The problem I am facing is when a user changes a number in the text box, how do I know which text box this is? I was looking to see if there was a attribute on RadNumericTextBox that could hold an arbitrary value such as my item key so I would know which number they changed. However, I don't see such an attribute.
Is there some way I can make a determination which text box they edited when I auto post back?
In case anyone asks, I do not want to force my user to click a button to make the row go into edit mode, change the number, then click a button to save the row.
You could do this with a Repeater control.
Include a RadNumericTextBox in the repeater's item template, and then write a server-side event handler for it. The client ID of the text box can be accessed through the event handler's sender object, but if that isn't enough information you can rely on the repeater's data source to relate whatever data you need with each text box.
The simplest way might be to use the Label attribute of the text box. Here's an example:
ASPX:
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<br />
<telerik:RadNumericTextBox ID="radNTB" runat="server" AutoPostBack="true" OnTextChanged="radNTB_TextChanged" ClientIDMode="Predictable"></telerik:RadNumericTextBox>
</ItemTemplate>
</asp:Repeater>
VB:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As New DataTable
dt.Columns.Add("Column1", GetType(String))
For i As Integer = 1 To 5
Dim row = dt.NewRow
row.Item("Column1") = "TextBox" & i.ToString
dt.Rows.Add(row)
dt.AcceptChanges()
Next
Repeater1.DataSource = dt
Repeater1.DataBind()
End If
End Sub
Private Sub Repeater1_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim tb As RadNumericTextBox = e.Item.FindControl("radNTB")
tb.Label = DataBinder.Eval(e.Item.DataItem, "Column1").ToString()
End If
End Sub
Public Sub radNTB_TextChanged(sender As Object, e As EventArgs)
Dim ntb As RadNumericTextBox = sender
Response.Write(ntb.Label)
End Sub
What you can do is by the item your binding the listview with the data source,
make the id of the RadNumericTextBox equals to your item key that you want to pass.
In the RadNumericTextBox TextChanged event cast the sender object to RadNumericTextBox type. in this
case you will get the unique item key you are looking for.
example :
<asp:FormView ID="frmViewPicture" runat="server">
<EditItemTemplate>
<telerik:RadNumericTextBox ID='Eval("ItemKey")'
OnTextChanged="radTxtNewPrice_TextChanged" AutoPostBack="true">
</telerik:RadNumericTextBox>
</EditItemTemplate>
</asp:FormView>
Make sure the item key is unique and available in your data source, other wise you will get an exception.
protected void radTxtNewPrice_TextChanged(object sender, EventArgs e)
{
Telerik.Web.UI.RadNumericTextBox txtRadNumericTextBox= (Telerik.Web.UI.RadNumericTextBox)sender;
var itemKey = txtRadNumericTextBox.ID;
// Do Your Logic Here
}
Hope this is helpful.
I have a checkbox that is part of a repeater. I'm trying to get the checkbox's checkedchanged event to occur when I check the box and when I uncheck the box. The event currently only triggers when I check the box however... not when i uncheck also. I'll post some code below and hopefully someone can steer me in the right direction. Thanks!
Adding Handler to Repeater CheckBox Control
Dim MyCheckBox As New CheckBox
MyCheckBox = e.Item.FindControl("MyCheckBox")
AddHandler MyCheckBox.CheckedChanged, AddressOf MyCheckBox_CheckedChanged
My CheckedChanged Handler Event
Private Sub MyCheckBox_CheckedChanged(sender As Object, e As System.EventArgs)
Dim RepeaterItem As RepeaterItem
For Each RepeaterItem In MyRepeater.Items
If IsListItem(RepeaterItem) Then
If CType(sender, CheckBox).Checked Then
CType(RepeaterItem.FindControl("SelectionCheckBox"), CheckBox).Checked = True
Else
CType(RepeaterItem.FindControl("SelectionCheckBox"), CheckBox).Checked = False
End If
End If
Next
End Sub
ASPX File Check Box Declaration
<asp:CheckBox ID="MyCheckBox" AutoPostBack="True" Text="" runat="server" />
Do you have AutoPostBack = true set up in your ASPX file.
I am having a strange day! I am trying to access values inside the Datagrid control. There are textboxes in each row of the Datagrid control. If I use a asp.net button control outside the datagrid control I can access all the values. But if I use LinkButton outside the datagrid control then I cannot access any value.
I have to use LinkButton.
UPDATE 1: Code
//This works
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim indexes() As Integer = dgReceipts.SelectedIndexNumbers
End Sub
//This does not work and I get 0 items in the SelectedIndexNumbers
Protected Sub LinkButtonUpdateReceipts_Click(sender As Object, e As EventArgs) Handles LinkButtonUpdateReceipts.Click
Dim indexes() As Integer = dgReceipts.SelectedIndexNumbers
End Sub
<asp:Button ID="Button1" runat="server" Text="Button" />
<c1:LinkButton ID="LinkButtonUpdateReceipts" runat="server" Text="Update Totals" Icon="images/go-blue-right.gif"></c1:LinkButton>
I'm suprised that either of those work. You do not appear to have an OnClick event for either of those buttons. At a minimum I would add an OnClick event to your LinkButton that fires LinkButtonUpdateReceipts_Click.
Also, why aren't you just using <asp:LinkButton? what is <c1:LinkButton?
I have an ASP.Net page, which displays a list of options to the user. When they select from the list, it does a post back and queries a sql server. The results are displayed in a listview below the options in an update panel. Below is a snippet of the ItemTemplate:
<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />
The DataItemIndex does not appear, so my commandargument is empty. However, the object sender is the button, which shows the item.
Why is the index item not appearing in the CommandArgument?
Could it be the post back? If so, why would it be the post back? Is there a way around it?
Edit:
Sorry, from my attempts to solve it before, I posted bad code, but it still isn't appearing.
Resolution:
I found another work around in that the sender of the OnCommand is the link button, which has the CommandArgument. I will chalk this issue up to be an issue with multiple postbacks and javascript.
You can't use the <%= %> syntax inside properties on a tag with a runat="server" attribute. I'm surprised the code will even run. :)
UPDATE:
You probably want to use the ItemDataBound event on the repeater, find the linkbutton and set the CommandArgument property.
Not very elegant, but here's a VB.NET sample.
Private Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim b As LinkButton = e.Item.FindControl("btn")
b.CommandArgument = e.Item.ItemIndex
b.DataBind()
End Select
End Sub
You're not setting it
You possibly want
<%# Container.DataItemIndex %>
or
<%= Container.DataItemIndex %>
:)
Try
<asp:LinkButton Text="Save IT" OnCommand="SaveIt" CommandArgument="<%# Container.DataItemIndex %>" runat="server" />
You were missing the "#" sign.
This site really helped me with this problem: http://forums.asp.net/t/1671316.aspx
The issue I ran into was that I was being passed null arguments in the commandargument when I clicked on the button a second time. As the post above explains, this is because commandargument is only set in the databind event. So, to fix this, include a databind event in the page_load sub
Ex. (VB)
Private Sub BindSelectButtons()
'Purpose: bind the data to the select buttons for commandargument to be used
Dim i As Integer
For i = 0 To gridview1.Rows.Count - 1
gridview1.Rows(i).Cells(8).FindControl("btnID").DataBind()
Next
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'Rebind select buttons so that the commandargument refreshes
BindSelectButtons()
End Sub
Make sure View State is enabled
e.Row.EnableViewState = true;