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") %>
Related
We have a page that allows users to update data using about a dozen textboxes and several dropdowns. Half of all the fields are permanently visible and the rest are hidden. If a checkbox is checked, the page reveals the rest of the fields.
When first arriving on the page, the two textboxes and dropdown in question are not visible, along with the rest of the fields not visible, unless the checkbox is checked.
However, with the update functionality (a button click) the two textboxes and dropdown appear with the other permanently visible fields. Oddly enough, the labels corresponding to the fields that should be hidden do not appear. Another point is that, if the checkbox is checked and then unchecked, the fields in question are not visible.
So it appears that the issue is with the update functionality but we haven't been able to pinpoint what the issue is. We have tried setting the visibility to false in the .ascx.cs file but that has had zero effect along with other things we've tried.
Is there anything obvious we might be missing from our code below?
ascx:
<asp:FormView ID="FormView1" runat="server" DataSourceID="odsQWER" DataKeyNames="Oid"
OnItemInserting="FormView1_ItemInserting" EnableModelValidation="true"
OnItemUpdating="FormView1_ItemUpdating">
...
<tr>
<td style="width: 200px">
<asp:Label ID="ContactFNameLabel" runat="server" Text="Vendor Contact (First Name):"
Visible="false"></asp:Label>
</td>
<td>
<asp:TextBox Width="400px" ID="ContactFNameTextBox" runat="server" Text='<%# Bind("FirstName") %>'
Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactFNameValidator" runat="server" ControlToValidate="ContactFNameTextBox"
Display="Dynamic" ErrorMessage="Contact Name cannot be Blank" Visible="false"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 200px">
<asp:Label ID="ContactLNameLabel" runat="server" Text="Vendor Contact (Last Name):"
Visible="false"></asp:Label>
</td>
<td>
<asp:TextBox Width="400px" ID="ContactLNameTextBox" runat="server" Text='<%# Bind("LastName") %>'
Visible="false"></asp:TextBox>
<asp:RequiredFieldValidator ID="ContactLNameValidator" runat="server" ControlToValidate="ContactLNameTextBox"
Display="Dynamic" ErrorMessage="Contact Last Name cannot be Blank" Visible="false"></asp:RequiredFieldValidator>
</td>
</tr>
<tr>
<td style="width: 200px">
<asp:Label ID="CountryLabel" runat="server" Text="Country:" Visible="false"></asp:Label>
</td>
<td>
<asp:DropDownList Width="405px" ID="CountryDropDownList" runat="server" AppendDataBoundItems="true"
DataSourceID="odsCountry" DataTextField="Name" DataValueField="Oid" SelectedValue='<%# Bind("CountryId") %>'
Visible="false">
<asp:ListItem Selected="true" Text="<--Select Here-->" Value="0"></asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="CountryValidator" runat="server" ControlToValidate="CountryDropDownList"
Display="Dynamic" ErrorMessage="Country Must be Selected" InitialValue="0"></asp:RequiredFieldValidator>
</td>
</tr>
...
<asp:Button ID="UpdateButton" runat="server" CausesValidation="true" CommandName="Update" OnClick="btnUpdateClick"
CssClass="button" Text="Update" Visible='<%# true %>' />
...
<asp:ObjectDataSource ID="odsQWER" runat="server" InsertMethod="AddRequest"
TypeName="BLL.TESTRequest" SelectMethod="GetRequest" OnInserting="odsQWER_Inserting"
UpdateMethod="UpdateRequest" OnUpdating="odsQWER_Updating" OnInserted="odsQWER_Inserted"
OnUpdated="odsQWER_Updated" OldValuesParameterFormatString="{0}">
...
aspx:
protected void btnUpdateClick(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)FormView1.FindControl("IsNewCheckBox");
FormView1.FindControl("ContactFNameTextBox").Visible = chk.Checked;
FormView1.FindControl("ContactFNameLabel").Visible = chk.Checked;
FormView1.FindControl("ContactFNameValidator").Visible = chk.Checked;
FormView1.FindControl("ContactLNameTextBox").Visible = chk.Checked;
FormView1.FindControl("ContactLNameLabel").Visible = chk.Checked;
FormView1.FindControl("ContactLNameValidator").Visible = chk.Checked;
FormView1.FindControl("StateDropDownList").Visible = chk.Checked;
FormView1.FindControl("StateLabel").Visible = chk.Checked;
}
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);
}
}
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.
I bind the repeater with SqlDataReader but I want when repater has no row than a blank row is added in ItedDataBound Event
1- First Create A DataTable Object to hold your data
2- Check for the Number of Rows in the DataTable if Zero then Add Empty DataRow Object To DataTable
3- Bind Your Repeater to the DataTable Instead to Datareader Object
if(dt.Rows.Count==0)
{
DataRow dr=dt.NewRow();
dt.Rows.Add(dr);
}
rptDemo.DataSource=dt;
rptDemo.DataBind();
Try this.
Place label in the footer of repeater in FooterTemplate and by default set visible to false and in ItemDataBound set to visible=true when item count is 0 or less than 1.
<asp:Repeater ID="rptDemo" runat="server"
OnItemDataBound="rptDemo_ItemDataBound">
<ItemTemplate>
.......
</ItemTemplate>
<FooterTemplate>
<%-- Label used for no data available --%>
<asp:Label ID="lblMsg" runat="server" CssClass="errMsg" Text="Sorry, no item is there to show." Visible="false">
</asp:Label>
</FooterTemplate>
protected void rptDemo_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (rptDemo.Items.Count < 1)
{
if (e.Item.ItemType == ListItemType.Footer)
{
Label lblFooter = (Label)e.Item.FindControl("lblMsg");
lblFooter.Visible = true;
}
}
}
Hope this helps!
My requirement was to show add row inside repeater. Me included a blank row as the last item by doing a small checking, in all other rows, blank row made hidden.
Used
<%# (((IList)((Repeater)Container.Parent).DataSource).Count).ToString() == (Container.ItemIndex + 1).ToString() %>
checking to decide whether to show or hide blank row.
Full code of view:
<table>
<asp:Repeater ID="repeater1" OnItemCommand="repeater_user_Itemcommand" runat="server">
<HeaderTemplate>
<tr>
<td>
Name
</td>
<td>
Email
</td>
<td>
Delete
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="lblName" runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"Name") %>'></asp:Label>
</td>
<td>
<asp:Label ID="lblEmail" runat="server" Text='<%# Eval("Email") %>'></asp:Label>
</td>
<td>
<asp:LinkButton ID="btnDelete" runat="server" CommandArgument='<%# Eval("ID") %>'
CommandName="delete">Delete</asp:LinkButton>
</td>
</tr>
<tr id="Tr1" runat="server" visible='<%# (((IList)((Repeater)Container.Parent).DataSource).Count).ToString() == (Container.ItemIndex + 1).ToString() %>'>
<td>
<asp:TextBox ID="txtName_add" runat="server" Enabled="True" Text='' Visible="false"></asp:TextBox>
</td>
<td>
<asp:TextBox ID="txtEmail_add" runat="server" Text='' Visible="false"></asp:TextBox>
</td>
<td>
<asp:LinkButton ID="btnShowAdd" runat="server" CommandName="add">Add</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
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