Currently I have an Ajax Timer which executes a function every few second. It is working perfectly fine until I tried to retrieve data from a DataTable. I have no idea why. I have tried debugging. I even placed label on the page to check.
For example the Ajax Timer:
Protected Sub Timer1_Tick(sender As Object, e As System.EventArgs) Handles Timer1.Tick
Label1.Text = DateTime.Now.ToLongTimeString()
End Sub
The datalist (the one giving the problem):
Protected Sub dlOrgProfile_ItemCreated(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlOrgProfile.ItemCreated
Dim bizLayerMgmt As BlOrganizations
Dim dt As DataTable
bizLayerMgmt = New BlOrganizations()
dt = bizLayerMgmt.getOrgDetails(userId).Tables(0)
ddl = CType(e.Item.FindControl("ddlCoType"), DropDownList)
Dim value As Integer = Convert.ToInt32(dt.Rows(0)(3)) 'I have narrowed the problem to this line, if I comment this line.. everything works perfectly
ddl.SelectedValue = value
End Sub
The problem lies in the dt.Rows(0)(3). I have no idea why. I need to use it to retrieve some data from the database.
Just in case if its the front-end side.. here's the markup for the site.
<div class="content">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:Timer ID="Timer1" runat="server" Interval="1000">
</asp:Timer>
<asp:DataList ID="dlOrgProfile" runat="server" DataSourceID="odsOrgDetails"
EnableTheming="True" RepeatLayout="Flow" ShowFooter="False" ShowHeader="False">
<ItemTemplate>
<h3>
<asp:TextBox ID="txBxCoName" runat="server" Text='<%# Eval("OrgName") %>'></asp:TextBox>
<div class="ddlSelect">
<asp:DropDownList ID="ddlCoType" runat="server" DataSource='<%# listOrgType() %>' DataTextField="OrganizationType" DataValueField="OrgTypeID" >
</asp:DropDownList>
</div>
<h3>
</h3>
<a id="linkCoImg" href="upload_co_logo.aspx">
<asp:Image ID="CoImg" runat="server" ImageUrl="~/logo/org/default.png" />
<span>Change</span> </a>
<br />
<div id="description">
<textarea id="taCoDesc" rows="2" cols="1"><%# Eval("Description") %></textarea>
</div>
<br />
<asp:Label ID="lblContacts" runat="server" Text="Contacts:"></asp:Label>
<br />
<div id="contacts">
<asp:TextBox ID="tbContactOffice" runat="server" CssClass="tbContacts"></asp:TextBox>
<asp:TextBox ID="tbContactFax" runat="server" CssClass="tbContacts"></asp:TextBox>
<asp:TextBox ID="tbContactMail" runat="server" CssClass="tbContacts2"></asp:TextBox>
</div>
<br />
<asp:Button ID="btnSave" runat="server" CommandArgument='<%# Eval("OrgID") %>'
CommandName="save" Text="Save" />
</h3>
</ItemTemplate>
</asp:DataList>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
</div>
Okay I have resolved my own problem.. Still don't really get it why but this is what I did.. Instead of using ItemCreated, I used ItemDataBound.
Protected Sub dlOrgProfile_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlOrgProfile.ItemDataBound
Dim bizLayerMgmt As BlOrganizations
Dim dt As DataTable
bizLayerMgmt = New BlOrganizations()
dt = bizLayerMgmt.getOrgDetails(userId).Tables(0)
ddl = CType(e.Item.FindControl("ddlCoType"), DropDownList)
Dim value As Integer = Convert.ToInt32(dt.Rows(0)(3))
ddl.SelectedValue = value
End Sub
Related
Dear all i have the following dropdownlist which is inside an update panel inside a repeater.
<asp:Repeater OnItemDataBound="rprProperties_ItemDataBound" ID="rprProperties" runat="server">
<ItemTemplate>
<div class="mb-2">
<asp:Label style="width : 100px;float:left;" ID="Label1" runat="server" Text='<%# Container.DataItem("name") %>'></asp:Label>
<asp:Label style="width : 100px;float:left;" ID="propID" runat="server" Text='<%# Container.DataItem("id") %>' Visible="false"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ClientIDMode="AutoID" AutoPostBack="true" OnSelectedIndexChanged="ddlProperty_SelectedIndexChanged" style="width:100px" CssClass="filter-dropdown bg-light" DataValueField="id" DataTextField="name" ID='ddlProperty' runat="server"></asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</ItemTemplate>
</asp:Repeater>
i'm populating the ddl with this code in the ItemDataBound event of the repeater.
Dim propID As Label = TryCast(e.Item.FindControl("propID"), Label)
Dim ddl As DropDownList = TryCast(e.Item.FindControl("ddlProperty"), DropDownList)
Dim varDbconn As New SqlConnection(ConfigurationManager.ConnectionStrings("shopCS").ToString)
Dim varDbcomm As SqlCommand
Dim varDbRead As SqlDataReader
varDbconn.Open()
varDbcomm = New SqlCommand("exec spShowItemPropValues #property,#id,#lang ", varDbconn)
varDbcomm.Parameters.AddWithValue("#property", SqlDbType.Int).Value = propID.Text
varDbcomm.Parameters.AddWithValue("#id", SqlDbType.Int).Value = Request.QueryString("id")
varDbcomm.Parameters.AddWithValue("#lang", SqlDbType.NVarChar).Value = Session("lang")
varDbRead = varDbcomm.ExecuteReader()
Dim varDt As New DataTable
varDt.Load(varDbRead)
ddl.DataSource = varDt
ddl.DataBind()
varDbcomm.Dispose()
varDbRead.Close()
varDbconn.Close()
when i select a value, the dropdownlist resets to the first item in the dropdownlist instead of keeping the selected value.
i want to retain that value.
thanks.
You forgot to add the AsyncPostBackTrigger. Put it before </UpdatePanel>
<asp:Repeater OnItemDataBound="rprProperties_ItemDataBound" ID="rprProperties" runat="server">
<ItemTemplate>
<div class="mb-2">
<asp:Label style="width : 100px;float:left;" ID="Label1" runat="server" Text='<%# Container.DataItem("name") %>'></asp:Label>
<asp:Label style="width : 100px;float:left;" ID="propID" runat="server" Text='<%# Container.DataItem("id") %>' Visible="false"></asp:Label>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:DropDownList ClientIDMode="AutoID" AutoPostBack="true" OnSelectedIndexChanged="ddlProperty_SelectedIndexChanged" style="width:100px" CssClass="filter-dropdown bg-light" DataValueField="id" DataTextField="name" ID='ddlProperty' runat="server">
</asp:DropDownList>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlProperty" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</div>
</ItemTemplate>
</asp:Repeater>
I have created a basic User Control with three drop down (Year,Month,Date) that exposes properties that
I wish to set from my code-behind page. This works fine
<%# Control Language="VB" AutoEventWireup="false" CodeFile="DateControl.ascx.vb" ClassName="DateControl"
Inherits="DateControl" %>
<%--<asp:ScriptManager ID="dd" runat="server">
</asp:ScriptManager>--%>
<div class="form-inline">
<div class="form-group">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlDay" OnSelectedIndexChanged="DDLActual_OnChange" runat="server" CssClass="form-control" />
<asp:DropDownList ID="ddlMonth" OnSelectedIndexChanged="DDLActual_OnChange" runat="server" CssClass="form-control" />
<asp:DropDownList ID="ddlYear" OnSelectedIndexChanged="DDLActual_OnChange" runat="server" CssClass="form-control" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
</div>
The problem started when i placed this usercontorl in repeater and try to access from codebehind on every item change by creating
method
<uc3:DateControl ID="txtDate" runat="server" Visible="false" OnChange="txtDate_PostBack" />
Protected Sub txtDate_PostBack(ByVal sender As Object, ByVal e As System.EventArgs)
Dim btndetails As DateControl = TryCast(sender, DateControl)
Dim ltrIsFormulaApplied As Literal = DirectCast(btndetails.FindControl("ltrIsFormulaApplied"), Literal)
End If
btndetails is showing nothing, i am not able to access values of other control, any help would be really appreciated..
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
im trying to edit a field using code instead of the wizard. im not entirely sure if the code i have is correct to update the field. here is the code i have to edit the field:
Protected Sub ListView1_ItemEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewEditEventArgs) Handles ListView1.ItemEditing
ListView1.EditIndex = e.NewEditIndex
ListView1.DataBind()
End Sub
Protected Sub ListView1_ItemUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewUpdateEventArgs) Handles ListView1.ItemUpdating
Dim profile = Request.QueryString("Profile")
Dim postid As Label = DirectCast(ListView1.EditItem.FindControl("postId"), Label)
Dim textbox As TextBox = DirectCast(ListView1.EditItem.FindControl("EditPostTxt"), TextBox)
Dim getComment = (From p In db.Posts Where p.PostId = New Guid(postid.Text)).Single
getComment.Post = cc.reverseExchangeSmilies(textbox.Text)
db.SubmitChanges()
ListView1.EditIndex = -1
cc.LoadComments(profile, ListView1)
End Sub
when ever i try to ether update or cancel the post because the post contains html i get the following error:
A potentially dangerous Request.Form value was detected from the client
i was wondering if before it updated the post it could use the reverseExchangeSmilies to turn them back in to smiles instead of there html or maybe allow html to be used at this point.
aspx page:
<asp:ListView ID="ListView1" runat="server">
<ItemTemplate>
<div id="header">
<asp:HyperLink ID="UserPageLik" runat="server" NavigateUrl='<%#"Default.aspx?Profile=" + Eval("ProfileId") %>'> <%# Eval("fullname")%> </asp:HyperLink><br />
</div>
<div id="leftcolumn">
<asp:ImageButton ID="Image1" runat="server" ImageUrl='<%#Eval("DisaplyPictureSmall") %>' /></div>
<div id="content">
<asp:Label ID="Label4" runat="server" Text='<%#Eval("Post") %>'></asp:Label><br />
</div>
<div id="footer">
<%# Eval("Date")%><br />
<asp:linkbutton id="linkbutton1" runat="server" CommandName="del" CommandArgument='<%# Eval("PostId") %>' forecolor="red" text="Delete" onclientclick="return confirm('Are you sure?');" />
<asp:linkbutton id="linkbutton2" runat="server" CommandName="Edit" CommandArgument='<%# Eval("PostId") %>' forecolor="red" text="Edit" />
</div>
<br />
</ItemTemplate>
<EditItemTemplate>
<div id="header">
<asp:Label ID="postId" runat="server" Text='<%#Eval("PostId") %>'></asp:Label>
<asp:HyperLink ID="UserPageLik" runat="server" NavigateUrl='<%#"Default.aspx?Profile=" + Eval("ProfileId") %>'> <%# Eval("fullname")%> </asp:HyperLink><br />
</div>
<div id="leftcolumn">
<asp:ImageButton ID="Image1" runat="server" ImageUrl='<%#Eval("DisaplyPictureSmall") %>' /></div>
<div id="content">
<asp:TextBox ID="EditPostTxt" runat="server" Text='<%#Eval("Post") %>' Width="100%" TextMode="MultiLine"></asp:TextBox>
</div>
<div id="footer">
<%# Eval("Date")%><br />
<asp:linkbutton id="SaveEditBut" runat="server" CommandName="Update" CommandArgument='<%# Eval("PostId") %>' forecolor="red" text="Update" />
<asp:linkbutton id="Linkbutton3" runat="server" CommandName="Cancel" CommandArgument='<%# Eval("PostId") %>' forecolor="red" text="Cancel" />
</div>
<br />
</EditItemTemplate>
</asp:ListView>
Thanks in advance.
The framework is preventing you from posting html code as a security measure. This can be turned off for the current page by adding a page directive.
<%# Page validateRequest="false" %>
The other option is to use javascript on the client side to change '<' to < and '>' to > and '&' to & before posting. Then on the server side you could decode the html before writing it to the screen.
function encodeValue(element_id)
{
var elem = document.getElementById(element_id);
var html = elem.value;
html= html.replace(/&/gi,"&");
html= html.replace(/</gi,"<");
html= html.replace(/>/gi,">");
elem.value = html;
}
I am using a datapager inside a nested Listview succesfully. When only one record of data is available the next previous last and first buttons are faded as per normal. However I would like them to not appear at all.
The aspx code I have is:
<asp:ListView ID="Pictures" runat="server" DataSourceID="SqlDataSource2" >
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<ItemTemplate>
<span style="">
<br />
<asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("ImageUrl") %>' ToolTip='<%# Eval("ToolTip") %>'
Height="150px" />
<br />
<asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("Description") %>' />
<br />
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div id="itemPlaceholderContainer" runat="server" style="">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="clear: both;">
<asp:DataPager ID="DataPager1" runat="server" PageSize="1">
<Fields>
<asp:NextPreviousPagerField ButtonType="Button" ShowFirstPageButton="True" ShowLastPageButton="True" />
</Fields>
</asp:DataPager>
</div>
</LayoutTemplate>
</asp:ListView>
The code behind that I have attempted to use is:
Dim pager As DataPager = CType(e.Item.FindControl("DataPager1"), DataPager)
If (Not pager Is Nothing) Then
pager.Visible = (pager.PageSize < pager.TotalRowCount)
End If
However the pager is always nothing in the debugger i.e it can't find the control.
This may be because I have not selected the event handler correctly (I've tried several) or the method may be wrong.
Any advice gratefully received.
Try it in the listview's databound event and also call the FindControl function from the listview.
Protected Sub Pictures_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles Pictures.DataBound
Dim pager As DataPager = CType(Pictures.FindControl("DataPager1"), DataPager)
If (Not pager Is Nothing) Then
pager.Visible = (pager.PageSize < pager.TotalRowCount)
End If
End Sub