Grab Label Text from ASP ListView in VB SelectedIndexChanged - asp.net

I have ListView1 with the following :
<ItemTemplate>
<tr style="text-align: center">
<td>Notification:
<asp:Label Text='<%# Eval("NotificationID") %>' runat="server" ID="NotificationIDLabel" Visible="False" />
<asp:Label Text='<%# Eval("CustomerID") %>' runat="server" ID="CustomerIDLabel" Visible="False" />
<asp:Label Text='<%# Eval("NotificationText") %>' runat="server" ID="NotificationTextLabel" /></td>
<td>
<asp:HyperLink ID="hlPromo" NavigateUrl='<%# Eval("URL") %>' ForeColor="#701A3C" runat="server">View</asp:HyperLink></td>
<td>
<asp:Button ID="btnDeleteNotification" runat="server" Text="Clear" ForeColor="#701A3C" BorderStyle="None" BackColor="#331700" /></td>
</tr>
</ItemTemplate>
I changed the btnDeleteNotification into a selected index change because I want to find out which NotificationID to delete in the SQL table. How could I grab the NotificationID of the selected row? I've tried every combination of VB I could think of to grab it.
Thanks in advance!

Use FindControl() inside of the SelectedIndexChanged event, like this:
Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim theNotificationLabel As Label = CType(ListView1.Items(ListView1.SelectedIndex).FindControl("NotificationIDLabel"), Label)
' Grab the ID from the text of the label
Dim theNotificationId As Integer = Convert.ToInt32(theNotificationLabel.Text)
End Sub
Note: If your list view is not named ListView1, then change it to whatever your list view is actually named.

Related

Repeater Autopostback Textbox Focus Position issue

I have a repeater control that contains a Textbox within an UpdatePanel, as well as other controls. The repeater fetches data from an SQL database and when the user makes changes to this specific textbox, the OnTextChange event is fired when the user "tabs out" of the text box. This event triggers a Stored Procedure code to update the databse with the new values in the textbox. The functionality I'd like to implement is when the user tabs out of the text box, the next text box in the repeater row should be focused on for continuous editing (similar to an excel spreadsheet) after the AutoPostBack from the OnTextChange event is fired. Would anyone have an example on how to do this using the code i have provided below? Thank you in advance!
Form Markup:
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:Repeater ID="GradeEditor" runat="server">
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Width="100%" Height="100%">
<table style="width:100%;">
<tr>
<td>
<asp:Label ID="LateLabel" runat="server" CssClass="label label-success" Visible="False"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" BackColor="Transparent" BorderColor="Transparent" OnTextChanged="Grade_TextChanged" style="text-align: center" Text='<%# Bind("Grade_Code") %>' Width="100%"></asp:TextBox>
</td>
</tr>
</table>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>' Visible="False"></asp:Label>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ID") %>' />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Class_ID") %>' />
<asp:HiddenField ID="HiddenField3" runat="server" Value='<%# Bind("Assignment_ID") %>' />
<asp:HiddenField ID="HiddenField4" runat="server" Value='<%# Bind("Student_ID") %>' />
<asp:HiddenField ID="HiddenField5" runat="server" Value='<%# Bind("Exempt") %>' />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</ItemTemplate>
</asp:Repeater>
Code Behind for Update:
Protected Sub Grade_TextChanged(sender As Object, e As EventArgs)
'Find the reference of the Repeater Item.
Dim ScoreText As TextBox = CType(sender, TextBox)
Dim item As RepeaterItem = CType(ScoreText.NamingContainer, RepeaterItem)
Dim ScoreID As Integer = Integer.Parse(TryCast(item.FindControl("HiddenField1"), HiddenField).Value)
Dim ClassID As Integer = TryCast(item.FindControl("HiddenField2"), HiddenField).Value.Trim()
Dim AssignmentID As String = TryCast(item.FindControl("HiddenField3"), HiddenField).Value.Trim()
Dim StudentID As String = TryCast(item.FindControl("HiddenField4"), HiddenField).Value.Trim()
Dim Grade As String = TryCast(item.FindControl("TextBox1"), TextBox).Text
Dim constr As String = ConfigurationManager.ConnectionStrings("AthenaConnectionString").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand("Scores_CRUD")
cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("#Action", "UPDATE")
cmd.Parameters.AddWithValue("#Score", Grade)
cmd.Parameters.AddWithValue("#ScoreID", ScoreID)
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
Me.BindRepeater()
End Sub
You have an issue in your markup with UpdatePanel. The UpdatePanel should have the Repeater control within its ContentTemplate as in markup below.
Change your code to as given below.
Correct Markup
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="True">
<ContentTemplate>
<asp:Repeater ID="GradeEditor" runat="server">
<ItemTemplate>
<asp:Panel ID="Panel1" runat="server" Width="100%" Height="100%">
<table style="width:100%;">
<tr>
<td>
<asp:Label ID="LateLabel" runat="server" CssClass="label label-success" Visible="False"></asp:Label>
</td>
<td> </td>
</tr>
<tr>
<td colspan="2">
<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" BackColor="Transparent" BorderColor="Transparent" OnTextChanged="Grade_TextChanged" style="text-align: center" Text='<%# Bind("Grade_Code") %>' Width="100%"></asp:TextBox>
</td>
</tr>
</table>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>' Visible="False"></asp:Label>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Bind("ID") %>' />
<asp:HiddenField ID="HiddenField2" runat="server" Value='<%# Bind("Class_ID") %>' />
<asp:HiddenField ID="HiddenField3" runat="server" Value='<%# Bind("Assignment_ID") %>' />
<asp:HiddenField ID="HiddenField4" runat="server" Value='<%# Bind("Student_ID") %>' />
<asp:HiddenField ID="HiddenField5" runat="server" Value='<%# Bind("Exempt") %>' />
</asp:Panel>
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Then, you need to emit JavaScript for focusing the next textbox from the text changed event of textbox in Repeater. This can be done using C# code as below.
Text changed event C# code for focusing the next box
protected void Grade_TextChanged(object sender, EventArgs e) {
//PUT your original code for calling stored procedure here
TextBox textBox = sender as TextBox;
BindRepeater();
RepeaterItem currentRepeaterItem = (RepeaterItem)(textBox.NamingContainer);
int currentRepeaterItemIndex = currentRepeaterItem.ItemIndex;
RepeaterItem nextRepeaterItem = null;
//get the next item row of Repeater
if ((rptCustomers.Items.Count - 1) >= (currentRepeaterItemIndex + 1)) {
nextRepeaterItem = GradeEditor.Items[currentRepeaterItemIndex + 1];
}
if (nextRepeaterItem != null) {
//get the textbox in next row of Repeater
TextBox nextTextBox = nextRepeaterItem.FindControl("TextBox1") as TextBox;
//emit JavasSript to focus the next textbox
ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "focusnext", String.Format(" var nextTextBox = document.getElementById('{0}'); nextTextBox.focus(); nextTextBox.select();", nextTextBox.ClientID), true);
}
}

ASP.net Listview dont show current/new value inserted

I have a problem in getting new entered record in the ListView.
In my list in ItemTemplate I replaced labels with TextBox. I have also created a button which loops over all the rows in ListView and shows the AcName field in alert message. If I update it (change the text of the AcName) to something else it still shows the record which is already present before, not the new text/value I entered. Below is the code which is working correctly for me but not generating the outcome I want, which is the new value.
HTML Markup
<asp:ListView ID="ListView1" InsertItemPosition="LastItem" runat="server" >
<ItemTemplate>
<tr>
<td>
<asp:Label runat="server" ID="lblID" Text='<%#Eval("ID") %>'></asp:Label>
</td>
<td>
<%--<asp:Label runat="server" ID="lblAcount" Text='<%#Eval("AcName") %>'></asp:Label>--%>
<asp:TextBox ID="lblAcount" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblNaration" Text='<%#Eval("Naration") %>'></asp:Label>--%>
<asp:TextBox ID="lblNaration" runat="server" CssClass="form-control" Text='<%#Bind("Naration") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblPaidAmount" Text='<%#Eval("PaidAmount") %>'></asp:Label>--%>
<asp:TextBox ID="lblPaidAmount" runat="server" CssClass="form-control" Text='<%#Bind("PaidAmount") %>'></asp:TextBox>
</td>
<td>
<%--<asp:Label runat="server" ID="lblDeductAmount" Text='<%#Eval("DeductionAmount") %>'></asp:Label>--%>
<asp:TextBox ID="lblDeductAmount" runat="server" CssClass="form-control" Text='<%#Bind("DeductionAmount") %>'></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="DeleteButton" cssClass="btn btn-info fa fa-trash-o" runat="server" CommandName="DeleteIt"></asp:LinkButton>
<td>
</td>
</tr>
<asp:Panel ID="Panel2" runat="server" Visible="False">
<tr>
<td>
<asp:Label ID="Label12" runat="server" Text="Credit Acount"></asp:Label>
<asp:TextBox ID="txtAcName" runat="server" CssClass="form-control" Text='<%#Bind("AcName") %>'></asp:TextBox>
</td>
</tr>
</asp:Panel>
</ItemTemplate>
</asp:ListView>
Button handler
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
For i As Integer = 0 To ListView1.Items.Count() - 1
Dim txtAcNames As TextBox = TryCast(ListView1.Items(i).FindControl("txtAcName"), TextBox)
Dim msg As String
msg = "<script language="'javascript'">"
msg += "alert('" & txtAcNames.Text & "');"
msg += "</script>"
Response.Write(msg)
Next
'txtTotal1.Text = Paid
End Sub
The above code works for me, the problem is when I enter data in the TextBox of the ListView it shows me the old values which is bound to the listview, not the new value that I enter.
what I want is it should show me the new value which I enter at runtime in the ListView TextBox.
You are alerting the txtAcName.Text at Button1_Click, but you are changing the value of lblAcount.Text at runtime. So, the old value you see is in fact from the other textbox.
Note that you have both textboxes binded to the same property <%# Bind("AcName") %>

How can I add and delete row on Telerik's RadListBox Control?

I'm using Telerik's RadListBox control for my web application development, wondering how can I add row into RadListBox via TextBox control, below is my code fragment:
<telerik:RadListBox ID="rlbControl" runat="server" SelectionMode="Multiple">
<ItemTemplate>
<table>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# Eval("Name") %>' ></asp:Label>
</td>
<td style="width:20px"></td>
<td >
<asp:Label ID="lblAge" runat="server" Text='<%# Eval("Age") %>' ></asp:Label>
</td>
</tr>
</table>
</ItemTemplate>
</telerik:RadListBox>
Name : <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
Age : <asp:TextBox ID="txtAge" runat="server" ></asp:TextBox>
<asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
<asp:Button ID="btnDel" runat="server" Text="Delete" OnClick="btnDel_Click"/>
When Add button click, get input from two TextBox and bind into RadListBox.
For deleting, select row from RadListBox and click on Delete button, remove the selected row from the RadListBox.
My question is how can I add and delete rows?
Thank you in advanced.
Hi you can create datatable and datarows. then add the textbox values to dararow. then u can bind this datatable to the RadListBox. For deleting, u need to pick the ListItem index and delete the row from the datatable and again bind to the RadListBox. It will work..
Link for delete Row demo
Also check with my answer in the below link post. you will get idea about insert row
Add rows to ListView

generate onclick event in image control in asp.net

i am have one datalist in asp.net
i m using vb with asp
now my code is just as follow
<table border="0" cellpadding="0" cellspacing="0" >
<tr>
<br />
<td>
Question Number:
<asp:Label ID="Label3" runat="server" Text='<%# Eval("Question_no") %>' />
<br />
Cust_id:
<asp:Label ID="Cust_idLabel" runat="server" Text='<%# Eval("Cust_id") %>' />
<br />
Question:
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Question") %>' />
<br />
Time:
<asp:Label ID="Label2" runat="server" Text='<%# Eval("Date_time") %>' />
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/ImagesFolde/Chrysanthemum.jpg" Height="50" Width="50" **OnClientClick="s()"**/>
all this in datalist as you can see
so it will be repeated..
i have put one image in it
now i want call a method when any image will be clicked
i make one sub called s() in vb
i want to call it
i use on clientclick() event but doesn't work
what to do?
you use onclientclick - have you made your functions with javascript/jquery ? ( the server side won't be fired)
for instance :
if you call you client method like this :
OnClientClick="return myFunction();"
then your function should be in the header as follows:
<script type=text/javascript>
function myFunction()
{
alert("activated here");
}
</script>
Suppose you have a datalist:
ASPX:
<asp:DataList id="ItemsList" OnItemCommand="Item_Command" runat="server">
<ItemTemplate>
<asp:LinkButton id="SelectButton" Text="Select" CommandName="ModifyRow"
runat="server" CommandArgument='<%#Eval("Id")%>' />
</ItemTemplate>
</asp:DataList>
VB (Code Behind) :
Here I am taking the id from '<%#Eval("Id")%>' and binding some products in bulleted list:
Protected Sub ItemsList_ItemCommand _
(source As Object, e As RepeaterCommandEventArgs) _
Handles Categories.ItemCommand
If e.CommandName = "ModifyRow" Then
' Determine the CategoryID
Dim categoryID As Integer = Convert.ToInt32(e.CommandArgument)
' Get the associated products from the ProudctsBLL and
' bind them to the BulletedList
Dim products As BulletedList = _
CType(e.Item.FindControl("ProductsInCategory"), BulletedList)
Dim productsAPI As New ProductsBLL()
products.DataSource = productsAPI.GetProductsByCategoryID(categoryID)
products.DataBind()
End If
End Sub
<asp:ImageButton ID="imgexpand" runat="server" ImageAlign="Bottom" ImageUrl="~/img/plus.png" OnClick="s" />
you do not need the () in the control onclick statement
this will fire the sub s() procedure in the vb code

ASP.NET: ListView display Image based on database column being Null or not

I have a database and I'm storing images in it along with a person's name, and other attributes. I've databound my listview with a stored procedure. I want to know how I can display an icon on a row depending on if the record for that row has a picture or not for the person...
I'm not sure how to accomplish this however with the templates in asp.net
If you need me to provide any additional info I can.
Thanks.
EDIT:
Here is my template and the s where I'm hoping to put them.
<ItemTemplate>
<tr style="">
<td class="width100">
<asp:Image ID="Image1" runat="server" ImageUrl="./Images/PlayerPictures/nothing.gif" title="Picture Available" CssClass="icon right" />
<asp:Image ID="Image2" runat="server" ImageUrl="./Images/PlayerPictures/nothing.gif" title="Charts Available" CssClass="icon right" />
<asp:Image ID="Image3" runat="server" ImageUrl="./Images/PlayerPictures/nothing.gif" title="Reports Available" CssClass="icon right" />
<asp:Image ID="Image4" runat="server" ImageUrl="./Images/PlayerPictures/nothing.gif" title="Video Available" CssClass="icon right" />
</td>
<td class="width350">
<asp:Label ID="PlayerLabel" runat="server" Text='<%# Eval("Player") %>' />
</td>
<td>
<asp:Label ID="PositionLabel" runat="server" Text='<%# Eval("Position") %>' />
</td>
</tr>
</ItemTemplate>
I have no code-behind that is relevant I don't think because the Listview is databound like so...
<asp:ListView ID="ListView4" runat="server"
DataSourceID="ListViewPlayersWithFilter"
DataKeyNames="PlayerKey">
You can use ListView.ItemDataBound Event to display an image based on the column value:
Aspx:
<ItemTemplate>
<tr style="">
<td class="width100">
<asp:Literal ID="lblPic" runat="server" />
...
...
</td>
<td class="width350">
<asp:Label ID="PlayerLabel" runat="server" Text='<%# Eval("Player") %>' />
</td>
<td>
<asp:Label ID="PositionLabel" runat="server" Text='<%# Eval("Position") %>' />
</td>
</tr>
</ItemTemplate>
Code Behind:
Protected Sub ListView1_ItemDataBound(ByVal sender As Object, ByVal e As ListViewItemEventArgs)
If e.Item.ItemType = ListViewItemType.DataItem Then
Dim lblPic As Literal = CType(e.Item.FindControl("lblPic"), Literal)
Dim rowView As System.Data.DataRowView
rowView = CType(e.Item.DataItem, System.Data.DataRowView)
If Convert.IsDbNull(rowView("hasPic") = true then
lblPic.Text = "<img src=\"/Images/PlayerPictures/nothing.gif\" alt=\"Picture Available\" CssClass=\"icon right\" />"
Else
lblPic.Text = "<img/> tag to Display Pic"
End If
End If
End Sub

Resources