How to pass TextBox value to DynamicPopulateExtender query? - c#

Hi I am trying to do DynamicPopulate on DropDownList2 when the value of textbox2 changes, how can I pass the value to TextBox2 to the sql query of DynamicPopulate.
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MYDB %>"
SelectCommand="SELECT Time FROM Tour WHERE (Date = TextBox2 )">
</asp:SqlDataSource>
<asp:DynamicPopulateExtender ID="DropDownList2_DynamicPopulateExtender"
runat="server" Enabled="True" PopulateTriggerControlID=""
TargetControlID="DropDownList2">
</asp:DynamicPopulateExtender>

add this javascript code to your page
<script type="text/javascript">
function UpdateControl(value) {
var behavior = $find('dp1');
if (behavior) {
behavior.populate(value);
}
}
</script>
and use this modified code:
<asp:DynamicPopulateExtender ID="DropDownList2_DynamicPopulateExtender"
runat="server" Enabled="True" PopulateTriggerControlID=""
TargetControlID="DropDownList2" BehaviorID="dp1">
</asp:DynamicPopulateExtender>
in code behind of your vb or C# page make sure to add the attribute
DropDownList2.Attributes.Add("onChange", "UpdateControl3(this.value);")
This will trigger the WebMethod for your DynamicPopulateExtender and will set the contextKey to the value of your dropdown.
Hope this helps.

I think you may be confused about what a DynamicPopulateExtender is used for. It's meant to return HTML from a call to a webservice when you change the value of a control on the page. It works with straight HTML, you don't use it to update other server controls on your page. I think what you're looking for is the CascadingDropDown, see http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/CascadingDropDown/CascadingDropDown.aspx for more details.

Related

Accessing C# session varible in ASP.Net

I am trying to access a session variable in aspx but I think there is an issue with my syntax somewhere.
Under page load:
Session["UserName"]= username.Substring(8).ToString(); // This is ok as far as I know.
Issue is with the asp bit:
<asp:TextBox ID="ATextBox" runat="server" Text="<% Session["UserName"] %>" />
<asp:TextBox ID="ATextBox" runat="server" Text="<%# Session["UserName"] %>" />
I have also tried the above (one at a time) with .ToString() at the end too. But I keep getting errors:
Either The server tag is not well formed or the server tag contains % %
One thing that might make a difference is the Textbox is inside a GridView, (it is inside a ContentTemplate)but not a boundfield.
Try this:
<asp:TextBox ID="ATextBox" runat="server" Text='<%# Session["UserName"] %>' />
Not sure if it would help..

Add ALL in Bound DropDownList

I currently developing a webpage using C#. I have a DropdownList data bound with the data from my sql database. After the dropdownlist bind with my database, the item inside the dropdownlist are userA, userB, and userC. If i select any of the item inside the dropdownlist, the data of the particular user will show in the gridview.
So, what I am trying to do now is I want to add an ALL into the dropdownlist. When I click on the ALL, the data of each user will be shown in the gridview. How can I achieve that? Any advice? Thanks.
P/S: I dont want to add any extra button in order to show all the user data. I want to make it in the dropdownlist.
This is my code:
WebApp.aspx:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
This is what i have edited for a new Webpage. I do not call the data binding function in code behind.
Thanks for any helps.
This is the answer i am looking for:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username" AppendDataBoundItems="True">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
Thanks everyone who is trying to help me.
You can achieve this:
Use this code after you bind the DropdownList with SQL data
ddlUsers.Items.Add(new ListItem("All", "all"));
ddlUsers.SelectedValue = "all";
Once this is done, you can make your select all user query based on this condition:
if(ddlUsers.SelectedValue == "all")
{
// your SQL query to select all users goes here.
}
In HTML Markup you can add this like:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
You can remove Selected="True" if you don't want this to be selected by default.
You can do few things to achieve this.
1)ddlUsers.Items.Add(new ListItem("All", "all")); add this line where ever you are binding the drop-down list.
2) use SelectedIndexChanged event of drop down like...
protected void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlUsers.SelectedValue == "all")
{
//Call your SQL query from here and bind the result set with your grid.
//if you need the id's of all the items in the drop down then write a loop and form a //string with , separated valued and pass it along.
}
}

Gridview textbox on Page_load enable edit

using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
N00b here,
I've got the gridview to look the way I want it to (a textbox inside of the ItemTemplate). The Textbox's class has some client-sideJS that enables a save button (an asp:LinkButton set to look like a Jquery UI save icon) to become visible after the Textbox's .keypress event fires..
Now for my question..I've looked everywhere, but I can't get how to have gridview put the Sql server db content in that textbox on Page_load (one textbox + <br /> for each row). I'm only printing one collumn from the Sql server db into the Gridview.. Also, how would I bind the asp:LinkButton save button to gridview's save event? If there is a more effecient way to do this? If you have some insight for me, please give me your opinion/!
My .aspx code
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="TextBox1" class="hexen" runat="server" DataField="TbValue" SortExpression="TbValue">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FluxConnectionString %>"
SelectCommand="SELECT [TbValue] FROM [InvestigateValues]">
</asp:SqlDataSource>
Thanks in advance!
Change your text box to
<asp:TextBox ID="TextBox1" class="hexen" runat="server" text='<%#Bind("TbValue")%>' />
This will enable two way databinding.
Here is an article to get you started: http://www.devx.com/DevX/Article/35058.
The grid view and SqlDataSource expose Insert, Update and Delete event/methods. These are on a row level, not grid level.
The way I would approach your problem would be to have an onclick event for your link button that iterates through the gridview, get the data from each text box and then perform the appropriate data base action in the code behind.

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.

Categories

Resources