Repeater -button click - asp.net

I have an Add button outside the Repeater control.When ADD IS Clicked a new row is inserted into database and databinded to repeater.
On click of ADD button i want a linkbutton to be showed in the new row of repeater.
Thanks in advance for the help.
DirectCast(e.Item.FindControl("lnksave"), LinkButton).Visible = True
This code i can not place in my ADD button's click event.what changes should i make to show the linkbutton in newly created row.

I would set LinkButton's property Visible="false" in the markup:
<asp:Repeater ...
... ... ...
<ItemTemplate>
<asp:LinkButton ID="lnksave" runat="server" Visible="false">LinkButton</asp:LinkButton>
</ItemTemplate>
In the code behind declare a flag at page level:
Dim btnClicked As Boolean = False
In Add button's event method set the flag to true. then data bind the repeater:
Protected Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
btnClicked = True
BindRepeater() 'your method to data bind repeater
End Sub
In the repeater's item databound event method check the flag and set the link button's visible property accordingly:
Protected Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles Repeater1.ItemDataBound
If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem) Then
Dim lnksave As LinkButton = DirectCast(e.Item.FindControl("lnksave"), LinkButton)
lnksave.Visible = btnClicked
End If
End Sub

Related

How to fire a button event from inside a repeater?

I have done my research but can't find an efficient way to do the following in VB:
Each button should fire the same event.
The button event saves every repeater item and so each event is not unique.
I am aware I can use the ItemCommand option but have not been able to get it working as desired.
ASP.NET
Inside Repeater Item
<asp:Button ID="btnSave" RunAt="Server"/>
VB.NET
Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sqlConn.Open()
For Each Item As RepeaterItem In rpt.Items
...
Next
sqlConn.Close()
End Sub
Edit:
After some research here on SO, I found that others events than ItemCommand are not caught by Asp:Repeater, as FlySwat said on his answer. So you'll need to write your VB.NET code like this:
First, declare the ItemCommand event on your page with something like this:
Protected Sub rpt_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.RepeaterCommandEventArgs) Handles rpt.ItemCommand
If e.CommandName = "Save" Then
'Save
End If
End Sub
Then, on Asp:Button markup inside the Asp:Repeater, you must set its CommandName property like this:
<Asp:Button ID="btnSave" runat="server" CommandName="Save" UseSubmitBehavior="false"/>
Take a look here to learn more about the UseSubmitBehavior.
Try it.
When the button is inside a Repeater template, you need to add OnClick event, you can add event on ItemDataBound event of the Repeater control.
Your .aspx code will look something like this:
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<asp:Button ID="btnSave" runat="server" Text="SomeText" />
</ItemTemplate>
</asp:Repeater>
code-behind
void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == Repeater1.AlternatingItem || e.Item.ItemType == Repeater1.Item)
{
var btn = e.Item.FindControl("btnSave") as Button;
if (btn != null)
{ // adding button event
btn.Click += new EventHandler(btn_Click);
}
}
}
void btn_Click(object sender, EventArgs e)
{
//write your code
}
in vb.net
Private Sub Repeater1_ItemDataBound(sender As Object, e As RepeaterItemEventArgs)
If e.Item.ItemType = Repeater1.AlternatingItem OrElse e.Item.ItemType = Repeater1.Item Then
Dim btn = TryCast(e.Item.FindControl("btnSave"), Button)
If btn IsNot Nothing Then
' adding button event
btn.Click += New EventHandler(btn_Click)
End If
End If
End Sub
Private Sub btn_Click(sender As Object, e As EventArgs)
'write your code
End Sub

ASP.NET: Why does my CheckedChanged event not execute when I "uncheck" the control?

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.

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

Button Click and LinkButton Click

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?

use a imageButton in GridView to select the row

I am trying to use a image button in a grid view to select a row so that i can then use the SelectedIndexChanged function to do other things. i have tried this:
<asp:ImageButton ID="Image1" CommandName="SelectRowGrid2" runat="server" ImageUrl="~/images/select.png" />
Protected Sub GridView2_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView2.RowCommand
Select Case e.CommandName
Case "SelectRowGrid2"
'some code for selecting the index?
Label1.Text = GridView2.SelectedIndex
End Select
End Sub
but it didn't even step in to the RowCommand sub when i went to debug it.
You could set GridView's AutoGenerateSelectButton to true MSDN
You could set LinkButton's CommandName to "Select" MSDN
Or you could try following:
Protected Sub ImgSelect_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs)
Dim row As GridViewRow = DirectCast(DirectCast(sender, ImageButton).NamingContainer, GridViewRow)
DirectCast(row.NamingContainer, GridView).SelectedIndex = row.RowIndex
End Sub
aspx:
<asp:TemplateField HeaderText="Select">
<ItemTemplate >
<asp:ImageButton ID="ImgSelect" OnClick="ImgSelect_Click" runat="server" />
</ItemTemplate>
</asp:TemplateField>
I have done this plenty times in c#. Maybe I can help you out.
Some things I do different than you:
I call CellContentClick opposed to RowCommand.
2. I think check to see, which cell in the row was clicked
3. Check that the row is not null
4. If it was my image button cell, get my row data
1.
private void dgv_CellContentClick(object sender, DataGridViewCellEventArgs e)
2.
if (e.ColumnIndex == dgv.Columns["btnClmn"].Index)
3.
if (dgvAllApplication.CurrentRow != null)
4.
txtName.Text = dgv.CurrentRow.Cells["App_Name"].Value.ToString();
Good Luck!
you can create on click event on the imagebutton and do whatever you need to do on that row from there
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
ImageButton img = (ImageButton)sender;
GridViewRow row = (GridViewRow)img.Parent.Parent;
//do stuff
//find label in the same row
Label lbl = (Label)row.FindControl("Label1");
}

Resources