How to get asp Dropdownlist selected value in c# file? - c#

I have a follow dropdown list in my aspx page:
<asp:DropDownList ID="OrderPeriodStatus" runat="server" AutoPostBack="true"
CssClass="ddlb" Width="100px" Height="30px" DataSourceID="SqlDataSource5"
DataTextField="OrderPeriod" DataValueField="OrderPeriodID" onselectedindexchanged="OrderPeriodStatus_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server" ConnectionString="<%$ ConnectionStrings:ProdDB %> # blah blah query
</asp:SqlDataSource>
I tried to access the value of selected value in C# by following code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ProcessEligibleScenarios();
LoadOptions();
ddlbPeriod.DataBind();
GridView1.DataBind();
gvActiveLogs.DataBind();
}
}
protected void OrderPeriodStatus_SelectedIndexChanged(object sender, EventArgs e)
{
if (OrderPeriodStatus.SelectedValue != null)
{
SqlConnection myConn = default(SqlConnection);
SqlCommand myComm = default(SqlCommand);
myConn = new SqlConnection(ConfigurationManager.ConnectionStrings["ProdDB"].ConnectionString);
myConn.Open();
myComm = new SqlCommand("SELECT OrderPeriodDate, OrderPeriodStatus, Notes FROM OrderPeriod WHERE OrderPeriod ='" + OrderPeriodStatus.SelectedValue + "'");
try
{
myComm.Connection = myConn;
SqlDataReader Dr = myComm.ExecuteReader();
while (Dr.Read())
{
System.Diagnostics.Debug.Write("While reading the data");
TextBox1.Text = Dr["OrderPeriodDate"].ToString();
TextBox2.Text = Dr["OrderPeriodStatus"].ToString();
NotesArea.Value = (string)Dr["Notes"];
System.Diagnostics.Debug.WriteLine(OrderPeriodStatus.SelectedValue);
}
Dr.Close();
myConn.Close();
}
catch (SqlException sqx)
{
}
GridView1.DataBind();
}
else { }
}
But when i print the value. It shows the selected Index not the value. Why?

You have mismatch error on sqdatasource, replace with SqlDataSource4
DataSourceID="SqlDataSource5" <---
<asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:ProdDB %> # blah blah query
</asp:SqlDataSource>

You can try get the value from Request.Form object and to get the text use ddl.Items.FindByValue method
var value = Request.Form[DropDownListnew.UniqeID];
var text = DropDownListnew.Items.FindByValue(value);

If you set AutoPostback property on the dropdownlist to true, and ViewStateMode to inherit, you should get value you selected. I tried it on my side, and it worked. Since you already have SqlDataSource, and the DataSourceId of the dropdownlist is set to the SqlDataSource, it should be populated automatically.
---edit
Sorry, I think I might have misread your question. If you instead use
DropDownListnew.SelectedItem.Text
you should get the text value you want.
-- another edit:
#Amit, you will get index from SelectedValue, because in your dropdownlist code, you have set
DataValueField="OrderPeriodID"
which is basically your primary key or index. To get the text value instead of index you have to use
SelectedItem.Text
Also in your query:
myComm = new SqlCommand("SELECT OrderPeriodDate, OrderPeriodStatus, Notes FROM OrderPeriod WHERE OrderPeriod ='" + OrderPeriodStatus.SelectedValue + "'");
I think you should be using
WHERE OrderPeriodID ='" + OrderPeriodStatus.SelectedValue + "'");
You are missing the ID bit after OrderPeriod.

Related

How do I add new DataSource to an already Databinded CheckBoxList

i'm building a web form that show Database's item(Tables, Rows, FK,...)
I have a CheckBoxList of Tables (chkListTable) which will show a new CheckBoxList of Rows (chkListRow) everytime I SelectedIndexChanged from chkListTable. The problem is i can show the items from chkListTable with 1 selected item. But i don't know how to show chkListRow if multiple item from chkListTable are selected.
Here are my codes:
aspx:
<div>
<asp:Label ID="Label2" runat="server" Text="Table: "></asp:Label>
<asp:CheckBoxList ID="chkListTable" runat="server"
DataTextField="name"
DataValueFeild="name"
AutoPostBack="true"
OnSelectedIndexChanged="chkListTable_SelectedIndexChanged">
</asp:CheckBoxList>
</div>
<div>
<asp:CheckBoxList ID="chkListRow" runat="server"
DataTextField="COLUMN_NAME"
DataValueField="COLUMN_NAME"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
</div>
aspx.cs:
protected void chkListTable_SelectedIndexChanged(object sender, EventArgs e)
{
tableName.Clear();
foreach (ListItem item in chkListTable.Items)
{
if(item.Selected)
{
tableName.Add(item.Text.Trim());
}
}
for(int i = 0; i < tableName.Count; i++)
{
String query = "USE " + dbname +
" SELECT * FROM information_schema.columns" +
" WHERE table_name = '" + tableName[i] + "'" +
" AND COLUMN_NAME != 'rowguid'";
chkListRow.DataSource = Program.ExecSqlDataReader(query);
chkListRow.DataBind();
Program.conn.Close();
}
}
Program.cs:
public static bool Connect()
{
if (Program.conn != null && Program.conn.State == ConnectionState.Open)
Program.conn.Close();
try
{
Program.conn.ConnectionString = Program.constr;
Program.conn.Open();
return true;
}
catch (Exception e)
{
return false;
}
}
public static SqlDataReader ExecSqlDataReader(String query)
{
SqlDataReader myreader;
SqlCommand sqlcmd = new SqlCommand(query, Program.conn);
sqlcmd.CommandType = CommandType.Text;
if (Program.conn.State == ConnectionState.Closed) Program.conn.Open();
try
{
myreader = sqlcmd.ExecuteReader();
return myreader;
myreader.Close();
}
catch (SqlException ex)
{
Program.conn.Close();
return null;
}
}
I want my display to be like this:
[x]Table1 [x]Table2 [ ]Table3
[ ]Row1(Table1) [ ]Row2(Table1) [ ]Row3(Table1)
[ ]Row1(Table2) [ ]Row2(Table2)
Ok, this is a rather cute little problem.
So, if we select 1 table, then we need to have one "child" or so called ONE check box list.
but, if we select 2 tables, (or 5), then we need 2 (or 5) child check box lists.
In other words, we would not use the same check box list (child), or try to "mangle" that "N" number of check box lists we need.
So, this "child" sets (one for each checked table) is NOT known ahead of time.
So, we have "repeating" set of those child check box lists, right?
So, we can (and should) then use a "repeater". All a repeater does is "repeat" the whatever we want, and we "feed" the repeater the main list of tables.
So, our markup will now look like this:
<style>
.rBut input {margin-right: 5px; }
.rBut label {margin-right: 15px; }
</style>
<asp:Label ID="Label2" runat="server" Text="Table: "></asp:Label>
<div class="rBut">
<asp:CheckBoxList ID="chkListTable" runat="server"
DataTextField="TABLE_NAME"
DataValueFeild="TABLE_NAME"
AutoPostBack="true" RepeatDirection="Horizontal" OnSelectedIndexChanged="chkListTable_SelectedIndexChanged" >
</asp:CheckBoxList>
</div>
</div>
<div>
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
<ItemTemplate>
Table: <%# Eval("Table") %> -
<div class="rBut">
<asp:CheckBoxList ID="chkListRow" runat="server"
RepeatDirection="Horizontal">
</asp:CheckBoxList>
</div>
</ItemTemplate>
</asp:Repeater>
Note for the 2nd repeter, we do NOT set the Value and text columns - WE DO NOT know them yet. You did not mention which columns to display, but most of my tables always have a PK "ID" for the first column, so lets make that the value , and the display (DataTextField, the 2nd column in the given table).
So, now we build up a "list" of the first selections, (tables), and then pass that thing to the repeater, and it will repeat.
the code now looks like this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
LoadData();
}
void LoadData()
{
string strSQL = "SELECT TABLE_NAME FROM VideoGames.INFORMATION_SCHEMA.TABLES " +
"WHERE TABLE_TYPE = 'BASE TABLE' ORDER BY TABLE_NAME";
chkListTable.DataSource = MyRst(strSQL);
chkListTable.DataBind();
}
protected void chkListTable_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable MyTables = new DataTable();
MyTables.Columns.Add("Table", typeof(string));
foreach (ListItem OneTable in chkListTable.Items)
{
if (OneTable.Selected)
{
DataRow OneRow = MyTables.NewRow();
OneRow["Table"] = OneTable.Value;
MyTables.Rows.Add(OneRow);
}
}
// ok, we have a list of tables, send that to repeater
Repeater1.DataSource = MyTables;
Repeater1.DataBind();
}
public DataTable MyRst(string strSQL)
{
var rst = new DataTable();
using (SqlConnection conn = new SqlConnection(Properties.Settings.Default.VideoGames))
{
using (SqlCommand cmdSQL = new SqlCommand(strSQL, conn))
{
conn.Open();
rst.Load(cmdSQL.ExecuteReader());
}
}
return rst;
}
protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item
| e.Item.ItemType == ListItemType.AlternatingItem)
{
DataRowView rItem = e.Item.DataItem as DataRowView; // get binding data row
CheckBoxList chkListRow = e.Item.FindControl("chkListRow") as CheckBoxList;
string strSQL = "SELECT * FROM " + rItem["Table"].ToString();
DataTable MyTable = MyRst(strSQL);
chkListRow.DataValueField = MyTable.Columns[0].ColumnName;
chkListRow.DataTextField = MyTable.Columns[1].ColumnName;
chkListRow.DataSource = MyRst(strSQL);
chkListRow.DataBind();
}
}
So, now we see this:
If I click one, then I see this:
But, say I click 3, then I see this:
So, note how I feed the Repeater a table (could have been sql query, but in this case, we create table in code). Pass it to repeater.
For each table, the itemdatabound triggers. Because there are multiple copies of the tables, then we need to use find control.
And if you wanted to test/get all of the checked items, then we do a for each on the Repeater items - and again use find control to get each check box control.
But note how we only have two check box lists, but the 2nd one is inside of that repeater, and is data driven.
As a result, this will work for 1, or "N" tables.

C# and ASP.NET - Populate web page textboxes from Dropdownlist using results in a SQL Table

I am trying to populate 2 text boxes using a dropdownlist on my web-page however I can't get the code required text boxes to populate
I can confirm that the code stated below works when I use a button instead of a dropdownlist
I have defined the dropdown list in the default.aspx as follows:
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Player" DataValueField="ID" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>`
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:Phocas_WorkBenchConnectionString %>" SelectCommand="SELECT ID, [PlayerFirstName] + ' ' + [PlayerLastName] as 'Player' FROM [Players]"></asp:SqlDataSource>
I have created the C# code in the default.aspx.cs as follows:
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
SqlConnection sql = new SqlConnection(#"Data Source=DESKTOP-PMSK135\SQLEXPRESS;Initial Catalog=Phocas_WorkBench;Integrated Security=True");
{
SqlCommand getFirstName = new SqlCommand("Select PlayerFirstName from Players where ID = #PlayerID", sql);
SqlCommand getLastName = new SqlCommand("Select PlayerLastName from Players where ID = #PlayerID", sql);
getFirstName.Parameters.AddWithValue("#PlayerID", DropDownList1.SelectedValue);
getLastName.Parameters.AddWithValue("#PlayerID", DropDownList1.SelectedValue);
sql.Open(); //Opens Connection to the SQL Database using the definded Connection String. In this case the defined connection string is stored in "sql"
string getResults = (string)getFirstName.ExecuteScalar();
TextBox3.Text = getResults;
getResults = (string)getLastName.ExecuteScalar();
TextBox4.Text = getResults;
sql.Close();
}
}
I am hoping to populate the results from the 2 SqlCommand into textbox3 and textbox4 with #PlayerID being equal to the results stored in the dropdownlist with the DataValueField="ID" as ID is the Unique identifier
I have tried many different types when trying to replace the variables in the AddWithValue. See the ones I have tried below:
DropDownList1.text
DropDownList1.DataValueField
DropDownList1.SelectedValue
DropDownList1.SelectedIndex
However none of the above seem to work.
A nudge in the right direction would be very helpful
Thank you.

DropDown Selected Value not Showing up

I have drop-down inside of a gridview so when the gridview is loaded and the drop-down is bound then the drop-down only show the first value of the drop-down list and it is not showing the previously selected value. When the gridview loads, i would like the drop-down to show what was previously selected for that row. Here is my code:
aspx markup for the drop-down:
<asp:TemplateField HeaderText="Answer">
<ItemTemplate>
<asp:Label ID="lblAns" runat="server" Text='<%# Eval("DDL_ANS")%>' Visible="false"></asp:Label>
<asp:DropDownList ID="ddl_Answer" runat="server">
</asp:DropDownList>
</ItemTemplate>
Here is code behind:
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddl_Answer;
//get current index selected
int current_quest = Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value);
ddl_Answer = e.Row.FindControl("ddl_Answer") as DropDownList;
using (SqlConnection con2 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["myconnection"].ConnectionString))
{
con2.Open();
using (SqlCommand cmd1 = new SqlCommand("select distinct DD_ANSWER from table1 where ID= '" + current_quest + "' ", con2))
{
ddl_Answer.DataSource = cmd1.ExecuteReader();
ddl_Answer.DataTextField = "DD_ANSWER";
ddl_Answer.DataValueField = "DD_ANSWER";
ddl_Answer.DataBind();
}
con2.Close();
}
}
I have tried to add this line of code after binding but i get this error "Object reference not set to an instance of an object"
ddl_Answer.Items.FindByValue((e.Row.FindControl("lblAns") as Label).Text).Selected = true;
thanks
I believe in your SELECT you need to use current_quest_sk instead of current_quest
Aslo try to check for null before accessing your controls:
var ddl_Answer = e.Row.FindControl("ddl_Answer") as DropDownList;
var answerLabel = e.Row.FindControl("lblAns") as Label;
if(answerLabel !=null && ddl_Answer!=null)
{
ddl_Answer.Items.FindByValue(answerLabel.Text).Selected = true;
}
#afzalulh has a valid point remove quotes if current_quest_sk(ID) is an Integer in your table.
You should avoid SQL injection but that's a different topic.
Place a breakpoint in your code, and setup through it with your debugger.
Either you have a typo in one of your string names or you are looking at the wrong control.
Stepping through your code will help you see exactly what line of your code is causing the problem.
You could also put a try/catch block around the whole thing to help you isolate the problem. Once you find the problem, remove the try/catch block.

Dynamic Formview with a dropdown list fails

188 Points
400 Posts
Dynamic Formview with a dropdown list fails horribly.
6 hours, 21 minutes ago|LINK
I've got concept in my head that I'm trying to materialize and it's not working. Here is what needs to happen: User selects a item from a list. Based on the selection a formview is built dynamically. The formview needs to be dynamic because the query will return a dataset with null values among non-null values such as this:
4600, 1, 4, NULL, NULL, 68 ....
The "4600" is model number and everything else is a ID that corresponds to a component. The Null values means that this filed does not belong to the 4600.
Henceforth, the formview then is build such that "4600" is fed to a label. For each non-null value I need to build a dropdown list, each with a separate datascource, not the ODS stuff, but a call to BLL class. Then the non-null value is assigned to the ddl's selected value property.
Simple enough, no? So here is the code and it's failing horribly. Actually just times out in an infinite loop. The time out happens in the ddlTonerBlack_DataBinding method. Can someone tell me what I'm doing wrong? Thanks. EJM
aspx markup:
<form id="form1" runat="server">
<div>
<asp:DropDownList runat="server" ID="ddlPrinterModels" AppendDataBoundItems="True"
DataTextField="Hardware_Model" DataValueField="Hardware_Model"
width="246px" CssClass="AssetMngnt-smallFont" AutoPostBack="true" >
<asp:ListItem Value="-1" Selected="True">-- Select Printer Model --</asp:ListItem>
</asp:DropDownList>
<hr />
<asp:PlaceHolder id="DetailsViewPlaceHolder" runat="server"/>
</div>
<!-- NOT A COMPLETE QUERY -->
<asp:sqldatasource id="ODSTonerBlackByModel"
selectcommand="SELECT [Hardware_Model], [Ident_Black], [Ident_Cyan], [Ident_Yellow] FROM [cPrinters_Toners] WHERE ([Hardware_Model] = #Hardware_Model)"
connectionstring="<%$ ConnectionStrings:CISF_Asset_Management %>"
runat="server">
<SelectParameters>
<asp:ControlParameter ControlID="ddlPrinterModels" Name="Hardware_Model"
PropertyName="SelectedValue" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</form>
Now the code file:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData_PrinterModels();
}
FormView printerModelFormView = new FormView();
dalConsumables_TonerBlack x = new dalConsumables_TonerBlack();
printerModelFormView.ID = "fvPrinterModel";
printerModelFormView.DataSourceID = "ODSTonerBlackByModel";
printerModelFormView.PagerSettings.Mode = PagerButtons.NextPrevious;
printerModelFormView.HeaderText = "Printer Model";
printerModelFormView.ItemTemplate = new FormViewTemplate();
DetailsViewPlaceHolder.Controls.Add(printerModelFormView);
}
protected void LoadData_PrinterModels()
{
Printer_ModelsList x = new Printer_ModelsList();
ddlPrinterModels.DataSource = x.GetPrinetr_Models();
ddlPrinterModels.DataBind();
}
protected void Page_Init(object sender, EventArgs e)
{
SqlDataSource sqlDS = new SqlDataSource();
sqlDS.ConnectionString = ConfigurationManager.ConnectionStrings["CISF_Asset_Management"].ConnectionString;
sqlDS.SelectCommand = "SELECT dbo.cCartridge_Black.Ident_Black, dbo.cCartridge_Black.Model_Black, " +
"dbo.cCartridge_Black.Desc_Black, dbo.cCartridge_Black.Qty_Black, " +
"dbo.cCartridge_Black.Black_Reorder_Limit, dbo.cCartridge_Black.Notes, " +
"dbo.cCartridge_Black.UpdatedBy, dbo.cPrinters_Toners.Hardware_Model " +
"FROM dbo.cCartridge_Black LEFT OUTER JOIN " +
"dbo.cPrinters_Toners ON dbo.cCartridge_Black.Ident_Black " +
"= dbo.cPrinters_Toners.Ident_Black";
form1.Controls.Add(sqlDS);
DropDownList ddl = new DropDownList();
ddl.ID = "ddlTonerBlack";
ddl.DataSource = sqlDS;
ddl.DataTextField = "Model_Black";
ddl.DataValueField = "Ident_Black";
form1.Controls.Add(ddl);
}
}
And the template class:
public class FormViewTemplate : System.Web.UI.ITemplate
{
void System.Web.UI.ITemplate.InstantiateIn(System.Web.UI.Control container)
{
Label lblPrinterModel = new Label();
lblPrinterModel.ID = "lblHardwareModel";
lblPrinterModel.DataBinding += new EventHandler(PrinterModelLabel_DataBinding);
container.Controls.Add(lblPrinterModel);
DropDownList ddlTonerBlack = new DropDownList();
ddlTonerBlack.ID = "ddlTonerBlack";
ddlTonerBlack.DataBinding +=new EventHandler(ddlTonerBlack_DataBinding);
container.Controls.Add(ddlTonerBlack);
}
private void PrinterModelLabel_DataBinding(Object sender, EventArgs e)
{
Label lblPrinterModel = (Label)sender;
FormView formViewContainer = (FormView)lblPrinterModel.NamingContainer;
DataRowView rowView = (DataRowView)formViewContainer.DataItem;
lblPrinterModel.Text = rowView["Hardware_Model"].ToString();
}
private void ddlTonerBlack_DataBinding(Object sender, EventArgs e)
{
DropDownList ddlTonerBlack = (DropDownList)sender;
FormView formViewContainer = (FormView)ddlTonerBlack.NamingContainer;
DataRowView rowView = (DataRowView)formViewContainer.DataItem;
dalConsumables_TonerBlack x = new dalConsumables_TonerBlack();
ddlTonerBlack.DataSource = x.GetListTonersBlack();
ddlTonerBlack.DataBind();
ddlTonerBlack.SelectedValue = rowView["Ident_Black"].ToString();
}
}
You get an infinite loop because in ddlTonerBlack_DataBinding method you call a DataBind method on the control that just fired a data bind event.
Simply you cause a databind event to fire in databind event handler of the same control and that's why you get an infinite loop.

Get Value From Dynamic Text Box ASP.NET

I am wondering how I can get the value from an asp.net textbox that is dynamically created inside an asp:datalist.
I would like to then insert these values back into the database.
Let say we have the following output on the web page from the datalist. (Each Question is a asp:textbox)
Question 1
Answer 1
Answer 2
Update Button
Question 2
Answer 1
Answer 2
Update Button
Question 3
Answer 1
Answer 2
Update Button
For example: I would like to get the value from Question 2 textbox and then parse the value to my database insert method.
How can this be done as the text boxes are generated dynamically in the data list?
I have written the following:
<asp:DataList runat="server" id="dgQuestionnaire" DataKeyField="QuestionID" CssClass="confirm">
<ItemTemplate>
<h3>Question <asp:Label ID="lblOrder" runat="server" Text='<%# Container.ItemIndex + 1 %>'></asp:Label></h3>
<asp:TextBox runat="server" ID="QuestionName" Text='<%# Eval("QuestionText") %>' CssClass="form"></asp:TextBox>
<asp:DataList ID="nestedDataList" runat="server">
<ItemTemplate>
<asp:TextBox ID="AnswerBox" runat="server" CssClass="form" Text='<%# Eval("AnswerTitle") %>' Width="300px"></asp:TextBox>
</ItemTemplate>
</asp:DataList>
<asp:Button runat="server" ID="updateName" CssClass="button_update" style="border: 0px;" onClick="UpdateQuestionName_Click" />
</ItemTemplate>
</asp:DataList>
And here the code behind (sorry about the length)
protected void UpdateQuestionName_Click(object sender, EventArgs e)
{
int QuestionnaireId = (int)Session["qID"];
GetData = new OsqarSQL();
// Update question name
GetData.InsertQuestions(QuestionName.Text, QuestionnaireId);
} // End NewQNRButton_Click
public void BindParentDataList(int QuestionnaireID)
{
_productConn = new SqlConnection();
_productConnectionString += "data source=mssql.myurl.com; Initial Catalog=database_2;User ID=userid;Password=aba123";
_productConn.ConnectionString = _productConnectionString;
SqlCommand myCommand = new SqlCommand("GetQuestion", _productConn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("#QUEST_ID", SqlDbType.Int));
myCommand.Parameters[0].Value = QuestionnaireID;
// check the connection state and open it accordingly.
_productConn.Open();
// Sql datareader object to read the stream of rows from SQL Server Database
SqlDataReader myDataReader = myCommand.ExecuteReader();
// Pass the Sql DataReader object to the DataSource property
// of DataList control to render the list of items.
dgQuestionnaire.DataSource = myDataReader;
dgQuestionnaire.DataBind();
// close the Sql DataReader object
myDataReader.Close();
// check the connection state and close it accordingly.
if (_productConn.State == ConnectionState.Open)
_productConn.Close();
// foreach loop over each item of DataList control
foreach (DataListItem Item in dgQuestionnaire.Items)
{
BindNestedDataList(Item.ItemIndex);
}
}
public void BindNestedDataList(int ItemIndex)
{
int QuestionID = Convert.ToInt32(dgQuestionnaire.DataKeys[ItemIndex]);
SqlCommand myCommand = new SqlCommand("GetAnswer", _productConn);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add("#QUESTION_ID", SqlDbType.Int).Value = QuestionID;
// check the connection state and open it accordingly.
if (_productConn.State == ConnectionState.Closed)
_productConn.Open();
// Sql datareader object to read the stream of rows from SQL Server Database
SqlDataReader myDataReader = myCommand.ExecuteReader();
// findControl function to get the nested datalist control
DataList nestedDataList = (DataList)dgQuestionnaire.Items[ItemIndex].FindControl("nestedDataList");
nestedDataList.DataSource = myDataReader;
nestedDataList.DataBind();
// close the Sql DataReader object
myDataReader.Close();
// check the connection state and close it accordingly.
if (_productConn.State == ConnectionState.Open)
_productConn.Close();
}
How can I get the value of a textbox when its corresponding button "updateName" is pressed?
Thanks
You can try doing something like this (code updated):
protected void UpdateQuestionName_Click(object sender, EventArgs e)
{
int QuestionnaireId = (int)Session["qID"];
GetData = new OsqarSQL();
//get the button that caused the event
Button btn = (sender as Button);
if (btn != null)
{
//here's you question text box if you need it
TextBox questionTextBox = (btn.Parent.FindControl("QuestionName") as TextBox);
// Update question name
GetData.InsertQuestions(questionTextBox.Text, QuestionnaireId);
//and in case you want more of the associated controls
//here's your data list with text boxes
DataList answersDataList = (btn.Parent.FindControl("nestedDataList") as DataList);
//and if answersDataList != null, you can use answersDataList.Controls to access the child controls, where answer text boxes are
}
} // End NewQNRButton_Click
This should work as you want.

Categories

Resources