Databind() from code behind never reaches the database - c#

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.

Related

Adding Blank Rows to GridView bound to SqlDataSource

I am creating a GridView that will initially have a blank row (consisting of 2 DropDownLists and one TextBox). There is a button in the footer that will allow a user to add another row after they have filled in the first. I am binding this to a SqlDataSource so I am able to use the InsertCommand to call a stored procedure to Insert the user's data into a database table. However, I am not sure how to go about adding an empty row. Right now, the headers will not show up until a postback call is executed. I have seen tutorials that use a DataTable to add an empty row, but I could not get this to work with the bound DropDownLists. Is there a way to add a row to the SqlDataSource from code behind? The following is the relevant part of my ASP page:
<table>
<tr>
<td colspan="2" style="width:100%">
<div>
<asp:UpdatePanel ID="upUpdateChecklist" runat="server">
<ContentTemplate>
<asp:GridView ID="gvUpdate" runat="server" AutoGenerateColumns="False"
CssClass="mGrid" PagerStyle-CssClass="pgr"
AlternatingRowStyle-CssClass="alt"
ShowHeaderWhenEmpty="true"
Visible="true" CellSpacing="2"
CellPadding="2" ShowFooter="true"
DataSourceID="dsUpdate">
<Columns>
<asp:TemplateField HeaderText="Division/Context">
<ItemTemplate>
<asp:DropDownList ID="ddDivision" runat="server" DataTextField="Division" DataValueField="ID" DataSourceID="dsDivision"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Application">
<ItemTemplate>
<asp:DropDownList ID="ddApplication" runat="server" DataTextField="Application" DataValueField="ID" DataSourceID="dsApplication" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Task">
<ItemTemplate>
<asp:TextBox ID="tbTask" runat="server" ReadOnly="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="btnAdd" Text="Add New Row" runat="server" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
</div>
</td>
</tr>
</table>
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsDivision" SelectCommand="SELECT ID, Division FROM Automation.dbo.Division ORDER BY Application, Division"
runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"></asp:SqlDataSource>
<asp:SqlDataSource ID="dsUpdate" runat="server"
ConnectionString="<%$ ConnectionStrings:AutomationDBConnectionString %>"
SelectCommand="">
<InsertParameters>
<asp:Parameter Name="Division" DbType="Int32" />
<asp:Parameter Name="Application" DbType="Int32" />
<asp:Parameter Name="Task" DbType="String" />
</InsertParameters>
</asp:SqlDataSource>
I am not completely sure I understand how do you intend to achieve what you want but if you want to generate an empty row, change the select command of the SQL data source to do a union with an empty dummy row.
<asp:SqlDataSource ID="dsApplication" SelectCommand="SELECT ID, ApplicationName FROM Automation.dbo.Applications UNION select -1 as ID, '' as ApplicationName "

populate a label from a listbox using panels/modal popup

I have created a listbox which is populated from a db. Using modal popup and panels, this listbox appears when the select user button is clicked. When a specific user is selected from this list and the add user button is clicked, I would like to populate the labels with the specific user name and userId. I cant seem to get the labels populated. It works when i dont use modal popup. Any ideas???
my code:
<asp:Label ID="UserId" runat="server"></asp:Label>
<asp:Label ID="UserName" runat="server" Font-Bold="true" ForeColor="#97b23c" Font-Size="14px"></asp:Label>
<br />
<asp:Button ID="SelectUserBtn" runat="server" Text="Select User" />
</asp:Panel>
</td>
<td>
<asp:Panel ID="Pnl" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<h4>List Of Available Users</h4>
<asp:ListBox ID="SourceList" runat="server" DataSourceID="SqlDataSource1"
DataTextField="FullName" DataValueField="UserId" Height="160" Width="200"></asp:ListBox><br />
<asp:Button ID="OKBtn" runat="server" Text="Add User" OnClick="OkBtn_Click" />
<asp:Button ID="CancelBtn" runat="server" Text="Cancel" /><br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:dmsConStr %>"
SelectCommand="SELECT [UserId], [FullName] FROM [UserProfiles]">
</asp:SqlDataSource>
</asp:Panel>
<asp:ModalPopupExtender ID="MPEUserList" runat="server" TargetControlID="SelectUserBtn" PopupControlID="Pnl" OkControlID="OKBtn" BackgroundCssClass="ModalBackground" DropShadow="true" CancelControlID="CancelBtn">
</asp:ModalPopupExtender>
my code behind:
protected void OkBtn_Click(object sender, EventArgs e)
{
UserId.Text = SourceList.SelectedItem.Value;
UserName.Text = SourceList.SelectedItem.Text;
}
Actually the ModalPopupExtender is preventing the form from postback. Just don't include OkControlID property and it should work well.
<asp:ModalPopupExtender runat="server"
ID="MPEUserList"
TargetControlID="SelectUserBtn"
PopupControlID="Pnl"
BackgroundCssClass="ModalBackground"
DropShadow="true"
CancelControlID="CancelBtn">
</asp:ModalPopupExtender>

C# Databind question

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();

Page still refreshes after wrapping the repeater in an update panel

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.

How to implement nested DropDownLists insid a GridView in asp.NET?

I have two nested DropDownLists (the value selected in the 1st ddl will limit the content of the 2nd ddl).
The example attached here runs fine outside of a GridView. As soon as I try to insert this into a GridView, I get an error because I cannot identify the ControlID to use in the ControlParameter.
The code outside of the GridView looks like this:
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
And the DataSources look like this:
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [Items]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [SubItems] WHERE [itemId]=:v_ItemId">
<SelectParameters>
<asp:ControlParameter Name="v_ItemId" ControlID="ACTION1_DROPDOWNLIST" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
Thanks!
I have tried something similar and it works fine. I can't tell from your post whether the data sources are together with the dropdowns inside the ItemTemplate of the GridView. At least the second one should be inside the ItemTemplate so that you will have a data source for each line in the grid.
I have posted my page below. It's using different tables, but it's the same idea.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Surname" DataValueField="FamilyID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="PersonID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Person] WHERE ([FamilyId] = #FamilyId)">
<SelectParameters>
<asp:ControlParameter Name="FamilyId" ControlID="ACTION1_DROPDOWNLIST"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Family]">
</asp:SqlDataSource>
</form>
The actual id's are build based on where they are, so you will see something like $GridView1 inserted in the id's.
To solve this you can set ClientIDMode="Static" on your page properties (first line of your .aspx file).
Or use "javascript:var a = document.getElementById('" + ACTION1_DROPDOWNLIST.ClientID + "');

Categories

Resources