I am trying to achieve the following:
Eliminate the FormView’s default edit link button.
Display the FormView in edit mode on Page_Load with current data and additional DDL data selections.
Currently I have the following to bind the DDL to the current database value based upon id = x etc:
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("xxx") %>'
DataSourceID="xxx"
DataTextField="xxx"
DataValueField="xxx"
ondatabound="DropDownList1_DataBound">
Via C# code I do this to add additional DDL list items for selection: (which is a waste of my energy because the data is already in the database!)
protected void DropDownList1 _DataBound(object sender, EventArgs e)
{
DropDownList DropDownList1 = (DropDownList)FormView1.FindControl("DropDownList1");
DropDownList1.Items.Insert(1, new ListItem("0 - 1 km/h", "0"));
DropDownList1.Items.Insert(2, new ListItem("2 - 5 km/h", "1"));
DropDownList1.Items.Insert(3, new ListItem("6 - 11 km/h", "2"));
DropDownList1.Items.Insert(4, new ListItem("12 - 19 km/h", "3"));
DropDownList1.Items.Insert(5, new ListItem("20 - 28 km/h", "4"));
}
There has got to be a better way!
Dont know exactly how you are connecting to the database but if you are using datacontext linq to sql is a good way to do it. Your 'ddl' will have an onLoad event and you can use a query in the code behind. For example:
<asp:DropDownList ID="DropDownList1" runat="server"
onLoad="DropDownList1_Load"
AutoPostBack="true" />
And then in the code behind:
protected void DropDownList1_Load(object sender, EventARgs e)
{
YourDataContext db = new YourDataContext();
var qry = from a in db.table1
select a;
foreach(var itm in qry)
{
DropDownList1.Items.Add(new ListItem(itm.speedRange.ToString(), itm.id.ToString()));
db.Dispose();
}
Related
I have a Web Form dependent on a Master Page, that contains a simple Form like this:
<asp:TextBox runat="server" ID="txtValue"></asp:TextBox>
<asp:DropDownList runat="server" ID="ddlProduct" />
<asp:Button runat="server" ID="btnConfirm" Text="Confirm" OnClick="btnConfirm_OnClick" />
on Page_Load I dynamically populate the DropDownList with values:
foreach(var product in products)
{
ListItem item = new ListItem(product.Nazev, product.ProductId.ToString());
ddlProduct.Items.Add(item);
}
Now .. normally if the DropDownList was populated statically, I would go with this, to get my selected value out of it:
protected void btnConfirm_OnClick(object sender, EventArgs e)
{
string selectedProduct = ddlProduct.SelectedValue;
}
But this is a no-go in this situation. Hence I try to directly get the selected value from POSTed parameters with one of those approaches (which are in fact practically the same):
protected void btnConfirm_OnClick(object sender, EventArgs e)
{
string selectedProduct = Request.Form.Get(ddlProdukt.ClientID);
string selectedProduct2 = Request.Params[ddlProdukt.ClientID];
}
But it doesn't work. If I debug it, the actual "id/name" of the DropDownList in the POSTed Form (Request.Params) is:
"ctl00$MainContent$ddlProdukt"
whereas the ClientId my approach gives me is:
"ctl00_MainContent_ddlProdukt"
I can't figure out, why does it replace '_' for '$' in the ID of my DDL control.. It seems to be such a trivial thing. There should be a better way to find out the right ID, than replacing the character, right?
Is there some other way I should "look/ask" for the selected value?
*Take into account, that I'am not looking for a solution with an UpdatePanel. I know, that using an UpdatePanel would result in the DDL value being sucessfully selected after the Postback, but that is NOT the answer I'm looking for here.
Thank you for any input.
If you still need to use the Request.Form.Get or Request.Params you should call it on ddlProdukt.UniqueID as it's separator is '$' versus '_' on the .ClientID
As requested an example. This code and the <asp:DropDownList ID="ddlProduct" runat="server"></asp:DropDownList> are on the aspx page, not the Master.
protected void Page_Load(object sender, EventArgs e)
{
//bind data not here
if (IsPostBack == false)
{
//but inside the ispostback check
ddlProduct.Items.Add(new ListItem("Item 1", "1"));
ddlProduct.Items.Add(new ListItem("Item 2", "2"));
ddlProduct.Items.Add(new ListItem("Item 3", "3"));
ddlProduct.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = ddlProduct.SelectedValue;
}
So I want to have a DDL (SELECT name FROM sys.databases (nolock)) that returns a value once selected (displayed in gridview possibly).
The problem that I have is that the query needs to make a connection to different databases depending on what the user selects from the DDL and return a value that is displayed in gridview. I want something like the below query:
How could I accomplish this?
use ([name]=#name) select sum(t.table1.column1) as Total from database inner join database2 on db1.table1.id= ([name]=#name).table2.id where([appname]=#appname) and Date <= GetDate()AND YEAR(Date) = year(GetDate())
I got this so far (not sure if this is the best method.) This is my code behind, how do I display the result using gridview that is dependent on DDL?
protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)
{
{
if (DropDownList2.SelectedValue == "test1")
{
SqlDataSource3.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test1ConnectionString"].ConnectionString;
{
SqlDataSource3.SelectCommandType = SqlDataSourceCommandType.Text;
SqlDataSource3.SelectCommand = "select top 10 * from table1 (nolock)";
}
}
else if (DropDownList2.SelectedValue == "test2")
{
SqlDataSource4.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["test2ConnectionString"].ConnectionString;
{
SqlDataSource4.SelectCommandType = SqlDataSourceCommandType.Text;
SqlDataSource4.SelectCommand = "select top 1 * from table1 (nolock)";
}
}
And below is my aspx code for the DDL.
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList2_SelectedIndexChanged">
<asp:ListItem>test1</asp:ListItem>
<asp:ListItem>test2</asp:ListItem>
</asp:DropDownList>
From what I understand you want to run different SQL when a user selects an item from the DropDownList. Anyway, you should put your code in the SelectedIndexChanged event of the control. As an example, your markup might look something like this:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
And in your code-behind:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
// call your sql
}
Edit
I've included a links to get your started. They show some code with the SelectedIndexChanged event.
http://www.devasp.net/net/articles/display/1294.html
http://www.c-sharpcorner.com/UploadFile/092589/working-with-dropdownlist-slectedindexchanged-event/
I have a TextBox within my EditItemTemplate in my listview_car:
<EditItemTemplate>
<asp:TextBox ID="txt1" runat="server" Text='<%# Bind("photo1") %>' Visibile="true">
</asp:TextBox>
</EditItemTemplate>
Now in the code I have this in my ItemUpdating event:
protected void listview_car_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
var txt1 = listview_car.Items[0].FindControl("txt1") as TextBox;
txt1.Text = "newImage";
}
Now I have debugged it and the value that is showing from my DB is correct, then when I set it from the code using txt1.Text = "newImage"; it shows it has updated the textbox in the Auto's however it does not update in the DB, but the strange thing is when I type in the textbox and click the edit button it updates but doesn't update when I set the string in the code?
Does anyone know what I am doing wrong?
When ItemUpdating() is called all values were already collected in the NewValues collection. To modify any of values you should modify e.NewValues
protected void listview_car_ItemUpdating(object sender, ListViewUpdateEventArgs e)
{
e.NewValues["key"] = "newImage";
}
where "key" is a name of the key (not a name of the control) which is usually a column name. You could also use an index 0,1,2...
In listview_car_ItemUpdating, first update the database, then call whatever method you used to bind your controls to the database when the page first loaded. This will retrieve the new value of photo1 from the database and bind it to txt1.
I need to bind an Asp.net DropDownList inside an ItemTemplate of a ListView. I am using LINQ to query data from using the LINQ db context as follow:
.cs
protected void ListView_AllTickets_ItemDataBound(object sender, ListViewItemEventArgs e)
{
DataClassesDataContext db = new DataClassesDataContext();
DropDownList ddl_spList = (DropDownList)e.Item.FindControl("DropDownList_SpList");
//Getting all service providers users
var spusers = (from x in db.User1s where x.usertype == "200" select x);
ddl_spList.DataSource = spusers;
ddl_spList.DataTextField = "email";
ListView_AllTickets.DataBind();
}
.aspx
<asp:DropDownList ID="DropDownList_SpList" runat="server" class="form-control" ClientIDMode="AutoID"> </asp:DropDownList>
Notice how I am finding the control then binding it to the result of the LINQ query. When I use the debugger, the data is retrieved successfully and the "email" field exists in the returned data. However, and for some reason, the ListView_AllTickets will have an item count of 0 even after the DataBind() statment.
You'll need to add this line:
ddl_spList.DataBind();
You're rebinding ListView_AllTickets, but that's the parent object and is already being bound (hence the event you're handling with this method). Is that a typo?
Bind ddl_spList instead.
1.I am creating a select tag in code behind page using a label control like:
Label1.Text += "< select >< option >one< /option>< option > two < /option >< option >three< /option>< /select>";
The += creates a number of select drop downs
i want to associates onSelectedIndexChanged Events on these Drop Downs and want to set Autopostback="true".
Please Help Me.
Try this one:
HTML Markup:
<asp:Label ID="Label1" runat="server"></asp:Label>
In your code-behind:
protected void Page_Load(object sender, EventArgs e)
{
DropDownList loDDL1 = new DropDownList();
loDDL1.ID = "ddl1";
loDDL1.AutoPostBack = true;
loDDL1.Items.Add(new ListItem("one", "1"));
loDDL1.Items.Add(new ListItem("two", "2"));
loDDL1.SelectedIndexChanged += new EventHandler(loDDL1_SelectedIndexChanged);
Label1.Controls.Add(loDDL1);
DropDownList loDDL2 = new DropDownList();
loDDL2.ID = "ddl2";
loDDL2.AutoPostBack = true;
loDDL2.Items.Add(new ListItem("three", "3"));
loDDL2.Items.Add(new ListItem("four", "4"));
loDDL2.SelectedIndexChanged += new EventHandler(loDDL2_SelectedIndexChanged);
Label1.Controls.Add(loDDL2);
}
void loDDL1_SelectedIndexChanged(object sender, EventArgs e)
{
// your code
}
void loDDL2_SelectedIndexChanged(object sender, EventArgs e)
{
// your code
}
There is a better way to achive what you are trying to do.
Create a dropdownlist in code behind and populate it there it self.
Add a placeholder for this dropdownlist on your webform.
Add the control using placeholder1.controls.add() function.
And set visibility as false for your label.
Now you have the reference of your dropdownlist just subscribe to its selected index changed event.