I've two SqlDataSources and two Repeaters, each repeater contains one hyperlink (i also tried using web server button and anchors).
The hyperlinks fetch from the database some values and in the NavigationUrl property I use a string.Format method to create a parameterized url, to pass for the browser, then second repeater is populated according to the value passed in the url which is originally passed by the first repeater's hyperlink
this is my sample code : https://gist.github.com/726213
<asp:ScriptManager id="Scrptmanagr" runat="server"></asp:ScriptManager>
<asp:UpdatePanel id="updtpanl" runat="server">
<ContentTemplate>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [arrange_by_id], [arrange_by] FROM [arrange_by]">
</asp:SqlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:HyperLink ID="HyperLink3" NavigateUrl='<%# string.Format("{0}?SortingType={1}",Request.AppRelativeCurrentExecutionFilePath, Eval("arrange_by_id"))%>' runat="server"><%# Eval("arrange_by") %></asp:HyperLink>
</ItemTemplate>
<SeparatorTemplate>
|
</SeparatorTemplate>
</asp:Repeater>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:testConnectionString %>"
SelectCommand="SELECT [alphabet_id],[arrange_by_id], [value] FROM [alphabet] WHERE ([arrange_by_id] = #arrange_by_id)">
<SelectParameters>
<asp:QueryStringParameter Name="arrange_by_id" QueryStringField="SortingType" Type="Int32" DefaultValue="1" />
</SelectParameters>
</asp:SqlDataSource>
<br /><br />
<asp:Repeater ID="Repeater2" runat="server" DataSourceID="SqlDataSource2">
<ItemTemplate>
<asp:HyperLink ID="hyper1" runat="server" NavigateUrl='<%#string.Format("{0}?SortingType={1}&SortBy={2}",Request.AppRelativeCurrentExecutionFilePath, Eval("arrange_by_id"),Eval("value"))%>'><%# Eval("value")%></asp:HyperLink>
</ItemTemplate>
<SeparatorTemplate>
|
</SeparatorTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
Now! everytime I click any of the hyperlinks it causes a full post back and refreshes the page! Am I missing something ?
Pretty sure an <asp:HyperLink> isn't going to get you a partial update, it renders to HTML as an <a href=".."> tag. You'll need a control that actually causes a postback, an <asp:Button> or <asp:LinkButton>.
Firstly any value you want to pass back don't use query strings, its the same page.
Place the content in hidden field or the buttons CommandArgument
<asp:HiddenField ID="hdnFieldName" Value='<%# Eval("columnName") %>' runat="server" />
then on the comand behind
protected void rptName_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if(e.CommandName.Equals("ButtonCommandName"))
{
RepeaterItem objItem = e.Item;
var objFieldValue = (HiddenField)objItem.FindControl("hdnFieldName");
}
}
And remember to set the update panel Mode="conditional' this will cause the updatepanel to only update when one of the following occurs:
1) If a control within the updatepanel causes a postback e.g. asp.net button.
2) If a trigger on the updatepanel occurs (about triggers: http://www.asp.net/web-forms/tutorials/aspnet-ajax/understanding-asp-net-ajax-updatepanel-triggers)
3) Finally if the "Update()" method is called
Otherwise, it won't update and refresh. When its set to always, any postback outside the updatepanel or another updatepanel can trigger the updatepanel to refresh.
Related
Pretty sure I know why I'm getting the error, just not sure how to go about fixing it. In a FormView ItemTemplate I've got a Label control for the Project ID# bound to the FormView's SQLDatasource. I've also got a DropDownList whose SQLDatasource I've got tied to the Label.Text as a ControlParameter. I am guessing since the Label control isn't bound yet, that's causing my DDL to kick out that error.
ASPX
<asp:FormView ID="FvChangeOrder" runat="server" DataKeyNames="ProjectChangeOrderID"
DataSourceID="FvChangeOrderSQL" OnItemCommand="FvChangeOrder_OnItemCommand"
OnDataBound="FvChangeOrder_OnDataBound">
<ItemTemplate>
<div>Project #: </div>
<div>
<asp:Label ID="LblProjectID" runat="server" Text='<%# Bind("ProjectID") %>' /></div>
<div>Shipment #: </div>
<div>
<asp:DropDownList runat="server" ID="DdlShipment" DataSourceID="DdlShipmentSQL"
SelectedValue='<%# Bind("ProjectShipmentID") %>' DataValueField="ProjectShipmentID"
DataTextField="ShipmentNo" Enabled="False" />
</div>
...
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="FvChangeOrderSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
SelectCommand="SELECT [ProjectID], [ProjectChangeOrderID], [ProjectShipmentID], [SeqNo],
[Date], [EnteredBy_UserID], [Source], [Initiator], [Reason], [ReasonNotes],
[ApprovalCode], [Description], [NumPanels], [Amount], [IsCommissionable], [DateDue],
[DateRecd], [Status]
FROM [tblProjectChangeOrder] WHERE ([ProjectChangeOrderID] = #PCOID)">
<SelectParameters>
<asp:QueryStringParameter Name="PCOID" QueryStringField="PCOID" Type="Int32"/>
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="DdlShipmentSQL" runat="server"
ConnectionString="<%$ ConnectionStrings:ProjectLogicTestConnectionString %>"
SelectCommand="SELECT pco.ProjectShipmentID, ps.ShipmentNo FROM tblProjectChangeOrder pco
LEFT JOIN tblProjectShipment ps ON pco.ProjectShipmentID = ps.ProjectShipmentID
WHERE pco.ProjectID = #ProjectID">
<SelectParameters>
<asp:ControlParameter ControlID="FvChangeOrder$LblProjectID"
Name="ProjectID" PropertyName="Text"/>
</SelectParameters>
</asp:SqlDataSource>
Would I be better off leaving out the WHERE and ControlParameter on the DDL SQLDatasource and changing the DDL's SQL Source in codebehind on the OnDataBound method of the FormView?
Think I got it worked out. I changed the ControlParameter to just Parameter Type="String" and on Form_Load set a Label to FvChangeOrder.FindControl("LblProjectID"), assigned LblProject.Text to a string, then passed that as the DefaultValue for the Select Parameter.
On an semi-related problem I had to remove the SelectedValue on the markup side and handle SQLCommand in the Form_Load for the DDL SelectedValue. Probably a bad dataset but I kept getting an error that the value in the field wasn't in the List or something like that. There are multiple Change Orders for a given project that have NULL values for the ShippingID.
Anywho, all is working on the ItemTemplate side. Now to see if I can keep everything working on the EditItemTemplate. :P
My FormView wont DataBind. I get no errors, all the elements are found correctly, when I step through the code everything looks like it works as expecting. The select parameter is set and the FormView is DataBound.
But no data is returned and my database logging shows that the procedure that is meant to be DataBound is never touched.
Update Panel
<asp:updatepanel ID="upnlMixingTankInfo" runat="server">
<ContentTemplate>
<asp:formview id="fvMixingTankInfo" runat="server" datasourceid="SqlDataSourceMixingTankInfo">
<ItemTemplate>
<asp:label runat="server">Vessel Capacity:</asp:label>
<asp:TextBox ID="vesselCapacity" runat="server" class="form-control" Text='<%# Bind("fldVesselCapacity")%>'></asp:TextBox>
</ItemTemplate>
</asp:formview>
</ContentTemplate>
Code Behind:
SourceDropDownList = sender
upnlMixingTankInfo = CType(SourceDropDownList.Parent.FindControl("upnlMixingTankInfo"), UpdatePanel)
fvTankInfo = CTYPE(upnlMixingTankInfo.FindControl("fvMixingTankInfo"), FormView)
If Not IsNothing(SourceDropDownList.SelectedValue) Then
SqlDataSourceMixingTankInfo.SelectParameters.Add("TankName", DropDownListEquipmentList.SelectedValue)
End If
fvTankInfo.Databind()
SQLDATSOURCE:
<asp:SqlDataSource ID="SqlDataSourceMixingTankInfo" runat="server"
ConnectionString="<%$ ConnectionStrings:ZMConnectionString %>"
SelectCommand="EXEC stpWebGetMixTankCapacity #TankName" >
<SelectParameters>
<asp:Parameter Name="TankName" defaultvalue=""/>
</SelectParameters>
Few changes in SqlDataSource and done, I've tested using my own sp, modify it according to your usage.
UpdatePanel & FormView
<asp:UpdatePanel ID="upnlMixingTankInfo" runat="server">
<ContentTemplate>
<asp:FormView ID="fvMixingTankInfo" runat="server" DataSourceID="SqlDataSource1">
<ItemTemplate>
<asp:Label ID="Label1" runat="server">Vessel Capacity:</asp:Label>
<asp:TextBox ID="vesselCapacity" runat="server" class="form-control" Text='<%# Bind("Name")%>'></asp:TextBox>
</ItemTemplate>
</asp:FormView>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind, I've tested on load, you can use same code wherever you want.
protected void Page_Load(object sender, EventArgs e)
{
SqlDataSource1.SelectParameters.Add("Id", "10");
}
SqlDataSource with changes:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="GetImages" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
It works perfectly, I've tested before submitting here.
OK, So Im fairly new to Web apps, been doing Windows app for a while in VB but Im switching to C# for web apps. Ok so I have a textbox and I have a SQLDataSource called SQLDataSource1. How in the world do I bind the source to the textbox so when it loads it will populate it?
Can someone give me some examples?
Ive tried the following,
Label1.Text = SqlDataSource1.SelectParameters.ToString();
But I get something other than the value of the SQLDataSource. I get this: System.Web.UI.WebControls.SqlDataSource
Thanks
I found an answer here on Forum.ASP.Net
Here is what has been said by Samu Zhang (Microsoft Online Community Support) :
It is hard to bind textbox to sqldatasource. We usually bind data controls such as FormView and GridView to sqldatasource and put one TextBox control in their Template . And you can invoke FindControl method of FormView to retrieve the textbox.
See my sample.
protected void Button1_Click(object sender, EventArgs e)
{
TextBox box = FormView1.FindControl("TextBox1") as TextBox;
}
<form id="form1" runat="server">
<div>
<asp:FormView ID="FormView1" runat="server" DataKeyNames="countryid" DataSourceID="SqlDataSource1">
<ItemTemplate>
countryid:
<asp:Label ID="countryidLabel" runat="server" Text='<%# Eval("countryid") %>'></asp:Label><br />
countryname:
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("countryname") %>'></asp:TextBox><br />
</ItemTemplate>
</asp:FormView>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
SelectCommand="SELECT * FROM [country]"></asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="retrieve value" />
</form>
One way is have your controls in a FormView or DetailsView, then set the DataSourceID of the FormView to the SqlDataSource and in your controls you bind them like so: asp:Label id=... Text='<%# DataBinder.Eval(Container.DataItem, "MyFieldName") %>' />, then on your OnInit of the page you can call MyFormView.DataBind();
Gridview has many columns and a Delete button as well. Delete button is a control as TemplateField
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="btnDelete" CommandName="Delete" Text='<%# Eval("disabled").ToString()=="False" ? "Disabled" : "Enabled" %>'
OnClientClick="return confirm('Are you sure you want to take this action?');"
runat="server"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
Now associated SQLDataSource's Delete command's stored procedure is expecting two parameters. One is going from DataKeyNames (RowID), other one i wanna pass is the Text of btnDelete (True or False).
How can i achieve it?
I would recommend doing it in the code behind. On btnDelete click i would iterate through every row in your gridview and check all the datakeynames. Once i found the one you want to delete you will need to send that back to you db. You can use either an orm like linq, ado.net, or a straight sqlcmd.
Please See this Code. I implement and works fine
<asp:TemplateField HeaderText="Add" HeaderStyle-CssClass="grid_Title">
<ItemTemplate>
<asp:HiddenField ID="HiddenField1" runat="server" Value='<%# Eval("TrackID")%>' />
<asp:SqlDataSource ID="SqlDataSource_Projects" runat="server" ConnectionString="<%$ ConnectionStrings:SentricMusicFunctionalityConnectionString2 %>"
SelectCommand="select* from MassTraxCatProjects where Fk_AgencyId=#CatalogAgencyId and fk_trackid=#TrackID) ">
<SelectParameters>
<asp:SessionParameter DbType="Int32" DefaultValue="CatalogAgencyId" Name="CatalogAgencyId"
SessionField="CatalogAgencyId" />
<asp:ControlParameter DefaultValue="TrackID" Name="TrackID" ControlID="**HiddenField1**" />
</SelectParameters>
</asp:SqlDataSource>
<asp:DropDownList ID="ddl_ProjectType" runat="server" DataSourceID="SqlDataSource_Projects"
Width="100px" DataTextField="ProjectName" DataValueField="ProjectId">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
I am trying to create a user control to wrap around the Membership API (A set of custom Gridviews to display the data better) and, while the code and the controls worked fine in the page, when I moved them to an .ascx, the events stopped firing to it.
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="CustomMembership.ascx.cs" Inherits="CCGlink.CustomMembership" %>
<asp:Panel ID="mainPnl" runat="server">
<asp:Label
id="lblError"
ForeColor="Red"
Font-Bold="true"
runat="server" />
<asp:GridView
id="grdUsers"
HeaderStyle-cssclass="<%# _headercss %>"
RowStyle-cssclass="<%# _rowcss %>"
AlternatingRowStyle-cssclass="<%# _alternatingcss %>"
OnRowUpdating="grdUsers_RowUpdating"
OnRowDeleting="grdUsers_RowDeleting"
OnRowCancelingEdit="grdUsers_cancelEdit"
autogeneratecolumns="false"
allowsorting="true"
AllowPaging="true"
EmptyDataText="No users..."
pagesize="<%# PageSizeForBoth %>"
runat="server">
<!-- ...columns... -->
</asp:GridView>
<asp:Button
id="btnAllDetails"
onclick="btnAllDetails_clicked"
text="Full Info"
runat="server" />
<asp:GridView
DataKeyNames="UserName"
HeaderStyle-cssclass="<%# _headercss %>"
RowStyle-cssclass="<%# _rowcss %>"
AlternatingRowStyle-cssclass="<%# _alternatingcss %>"
id="grdAllDetails"
visible="false"
allowsorting="true"
EmptyDataText="No users in DB."
pagesize="<%# PageSizeForBoth %>"
runat="server" />
<asp:Button
id="btnDoneAllDetails"
onclick="btnAllDetails_clicked"
text="Done."
Visible="false"
runat="server" />
</asp:Panel>
However, none of the events in the first two controls (the gridview grdUsers and the button btnAllDetails) simply do NOT occur, I have verified this in the debugger. If they occured just fine in the aspx page, why do they die on moving to the ascx?
My code in the aspx now is:
<div class="admin-right">
<asp:ScriptManager ID="sm1" runat="server" />
<h1>User Management</h1>
<div class="admin-right-users">
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<cm1:CustomMembership
id="showUsers"
PageSizeForBoth="9"
AlternatingRowStylecssclass="alternating"
RowStylecssclass="row"
DataSource="srcUsers"
HeaderStylecssclass="header"
runat="server" />
</ContentTemplate>
</asp:UpdatePanel>
</div>
Thanks.
You've not said whether you're building a Web Site or a Web Application - if you're building a web application have you checked that the .designer file has updated properly when you moved the controls into the user control?
http://forums.asp.net/t/1102183.aspx
This explains that there is an issue with ButtonType="Image" in the CommandField. Changing the entry to "Link" fixes this problem.