List of controls in code behind - asp.net

How can I add to a list<> some controls of my dashboard? I do something like List<String> NewList = new List<Strings>(){} but this throw me errors
I'm trying to add some linkButtons into the list.
I'm tying to do something like this but is wrong
protected void listLink(){
List<LinkButton> linksList = new List<LinkButton>();
listLink{
}
}
I want to use that list in other events
my linkbuttons are in visible = false;

From what i understand you want to create a list of LinkButtons using Object Initialization way.
Below code snippet should help you.
Aspx code:
<form id="form1" runat="server">
<asp:LinkButton ID="lnkBtn1" runat="server"></asp:LinkButton>
<asp:LinkButton ID="lnkBtn2" runat="server"></asp:LinkButton>
</form>
Aspx.cs code:
List<LinkButton> lnkBtnCollection = null;
protected void Page_Init(object sender, EventArgs e)
{
//Object Initialization way
lnkBtnCollection = new List<LinkButton>
{
lnkBtn1, lnkBtn2
};
//Second way where items are added to the list after the list is created
//lnkBtnCollection = new List<LinkButton>();
//lnkBtnCollection.Add(lnkBtn1);
//lnkBtnCollection.Add(lnkBtn2);
}
protected void Page_Load(object sender, EventArgs e)
{
//Use LinkBtn Collection here
foreach (var lnkBtn in lnkBtnCollection)
{
}
}

Related

C# - Event handler for dynamic buttons

I have a textbox and submit button created using the design mode.
When the submit button is pressed, it will retrieve the user input from the textbox and then make a query to my database.
It will then display a list of dynamic buttons according to the information retrieved from my database.
However, the event handler for the buttons does not fire when clicked. I guess my problem is the postback but I cannot create those buttons in page_load etc. because I need to get the user input (from the textbox when submit button is pressed) before i can load the buttons.
How can i solve this problem?
Thank you.
Edit (codes):
protected void subBtn_Click(object sender, EventArgs e)
{
//database setup codes
.......
while (reader.Read())
{
Button detailsBtn = new Button();
detailsBtn.Text = reader["fName"].ToString();
//doesn't fire
detailsBtn.Click += new EventHandler(detailsBtn_Click);
memPanel.Controls.Add(detailsBtn);
}
}
Main problem is Postback regenerate dynamic controls on each postback if those controls does not exists.
For quick demo see this code
ASPX CODE
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Panel ID="pnl" runat="server"></asp:Panel>
</form>
ASPX.CS CODE
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
generate();
}
}
public void generate()
{
if (!pnl.HasControls())
{
for (int i = 0; i < 4; i++)
{
Button detailsBtn = new Button();
detailsBtn.Text = "fName" + i.ToString();
detailsBtn.ID = i.ToString();
detailsBtn.Click += new EventHandler(detailsBtn_Click);
pnl.Controls.Add(detailsBtn);
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
generate();
}
protected void detailsBtn_Click(object sender, EventArgs e)
{
}
Sound to me like you could easily refactor your page to use a simple <asp:Repeater runat="server" ..></asp:Repeater> instead of dynamically adding controls to a Panel.
Here is a very simple complete sample:
RepeaterTest.aspx
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
<asp:TextBox ID="theTextBox" runat="server"></asp:TextBox>
<asp:Button ID="theButton" runat="server" OnClick="theButton_Click" Text="Click me" />
<asp:Repeater ID="test" runat="server">
<ItemTemplate>
<asp:Button ID="theRepeaterButton" runat="server" Text='<%# Eval("fName") %>' OnClick="theRepeaterButton_Click" />
</ItemTemplate>
</asp:Repeater>
</asp:Content>
RepeaterTest.aspx.cs
public partial class RepeaterTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void theButton_Click(object sender, EventArgs e)
{
string filter = theTextBox.Text;
// below row simulates fetching data using the filter text in the text box
var data = Enumerable.Range(0, 20).Select(i => new { fName = filter + " " + i });
test.DataSource = data;
test.DataBind();
}
protected void theRepeaterButton_Click(object sender, EventArgs e)
{
var button = (Button)sender;
// do something here based on text/commandname/commandargument etc of the button
}
}

Radio button doesn't get selected after a post back

I have an item template within repeater:
<ItemTemplate>
<li>
<input type="radio"
value="<%# GetAssetId((Guid) (Container.DataItem)) %>"
name="AssetId"
<%# SelectAsset((Guid) Container.DataItem) %> />
</li>
</ItemTemplate>
I have a method that compares ids and decides whether to check the radio button.
protected string SelectAsset(Guid uniqueId)
{
if (uniqueId == GetSomeId())
return "checked=\"checked\"";
return string.Empty;
}
SelectAsset gets hit, but it doesn't select a radio button on a post back, but it does work if I just refresh the page. What am I doing wrong here?
Answer here: How to display "selected radio button" after refresh? says that it's not possible to achieve, is this really the case?
Thank you
Update
It appears that view state isn't available for simple controls if they don't have a runat attribute. I have solved this by using a custom GroupRadioButton control. Thank you for your help.
I'd suggest using a RadioButtonList:
Page Code
<asp:RadioButtonList RepeatLayout="UnorderedList" OnSelectedIndexChanged="IndexChanged" AutoPostBack="true" ID="RadioRepeater" runat="server" />
<asp:Label ID="SelectedRadioLabel" runat="server" />
Code Behind
if (!Page.IsPostBack)
{
/* example adds items manually
- you could iterate your datasource here as well */
this.RadioRepeater.Items.Add(new ListItem("Foo"));
this.RadioRepeater.Items.Add(new ListItem("Bar"));
this.RadioRepeater.Items.Add(new ListItem("Baz"));
this.RadioRepeater.SelectedIndex = this.RadioRepeater.Items.IndexOf(new ListItem("Bar"));
this.RadioRepeater.DataBind();
}
protected void IndexChanged(object sender, EventArgs e)
{
this.SelectedRadioLabel.Text = string.Format("Selected Item Text: {0}", this.RadioRepeater.SelectedItem.Text);
}
I assume you only need to select one item.
As described in the comments, it even works to access the SelectedItem in the Page_Loadevent handler:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
// previous code omitted
}
else
{
string foo = this.RadioRepeater.SelectedItem.Text;
}
}
If you are creating all your controls dynamically at run-time (directly from code), then things are a little different. Here is the code that I used:
Page Code
<form id="form1" runat="server">
</form>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
RadioButtonList rbl = new RadioButtonList();
rbl.AutoPostBack = true;
rbl.SelectedIndexChanged += rbl_SelectedIndexChanged;
rbl.Items.Add("All");
// generate your dynamic radio buttons here
for (int i = 0; i<5; i++)
{
rbl.Items.Add(string.Format("Dynamic{0}", i));
}
form1.Controls.Add(rbl);
if (!Page.IsPostBack)
{
rbl.SelectedValue = "All";
PopulateTextBox(rbl.SelectedValue);
}
}
void rbl_SelectedIndexChanged(object sender, EventArgs e)
{
RadioButtonList foo = (RadioButtonList)sender;
PopulateTextBox(foo.SelectedValue);
}
void PopulateTextBox(string selection)
{
TextBox box = new TextBox();
box.Text = selection;
form1.Controls.Add(box);
}

Dynamically accessing array variables from Code Behind in ASPX

Due to the nature of the current file system I am working with I have to dynamically access array variables in ASPX page from the code-behind page. I got it to the point where I am able to pull any one explicitly stated variable, but cannot seem to pull them dynamically.
Code Behind:
public partial class some_class : System.Web.UI.Page {
public string[] array123 = new string[100];
....
protected void Button1_Click(object sender, EventArgs e) {
someFunction();
}
protected void someFunction() {
int i = 1;
_TempDt = Locator._New_Locator(value)
foreach (DataRow _TempDR in _TempDt.Rows) {
array123[i] = Server.UrlEncode(address);
i++;
}
}
}
ASPX:
....
<asp:Repeater ID="DataList" runat="server">
<ItemTemplate>
<label onClick="javascript:popup('page.aspx?key=<%= array123[1] %>')">Get link</label>
</ItemTemplate>
</asp:Repeater>
This only pulls the stated (2nd) value in the array and it needs to be dynamic with the repeater.
Thanks.
I think this is what you are looking for:
<p><%# array123[Container.ItemIndex] %></p>
Try this:
< %#DataBinder.Eval(Container, "ItemIndex", "")%>

asp.net web forms best practice to retrieve dynamic server control values

I populate web form with dynamic list of exams from database. I want user to enter examination marks for each exam. There is list of exam titles and textbox near each title.
I create list with repeater control (ViewState is disabled) :
<asp:Repeater ID="rptExams" runat="server" onitemdatabound="rptExams_ItemDataBound" >
<ItemTemplate>
<tr>
<td>
<asp:Literal runat="server" ID="ltTitle"/>
</td>
<td>
<asp:HiddenField runat="server" ID="hfId"/>
<asp:Textbox runat="server" ID="tbMark"/>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
And bind data to repeater on page_init:
class Exam
{
public int Id { get; set;}
public string Title { get; set;}
}
...
// this list is retrieved from database actually
Exam[] Exams = new Exam[]
{
new Exam { Id = 1, Title = "Math"},
new Exam { Id = 2, Title = "History"}
};
...
protected void Page_Init(object sender, EventArgs e)
{
rptExams.DataSource = Exams;
rptExams.DataBind();
}
So far so good. Then I have to retrieve data on postback. I have two ways but both of them looks ugly.
Idea is to store dynamically created databounded controls on ItemDataBoundEvent in Page_Init stage, and process their values in Page_Load stage. It looks like this:
private Dictionary<HiddenField, TextBox> Id2Mark = new Dictionary<HiddenField, TextBox>();
protected void rptExams_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
...
if (IsPostBack)
{
var tbMark = (TextBox)e.Item.FindControl("tbMark");
var hfId = (HiddenField)e.Item.FindControl("hfId");
// store dynamically created controls
Id2Mark.Add(hfId, tbMark);
}
...
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (var pair in Id2Mark)
{
int examId = Int32.Parse(pair.Key.Value);
string mark = pair.Value.Text;
// PROCESS
}
...
I'm completely sure there is a better way to retrieve data from dynamically created controls. Thank you in advance!
Here's how you can do it:
First, don't rebind the data on postback - it's not necessary. Only bind it on the first call of the page.
protected void Page_Init(object sender, EventArgs e)
{
if (!IsPostBack){
rptExams.DataSource = Exams;
rptExams.DataBind();
}
}
You won't need the Dictionary neither.
Then, on a postback, you can get to the bound data as follows:
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
foreach (RepeaterItem item in rptExams.Items)
{
HiddenField hfId = item.FindControl("hfId") as HiddenField;
TextBox tbMark = item.FindControl("tbMark") as TextBox;
int examId = Int32.Parse(hfId);
string mark = tbMark.Text;
// PROCESS
}
}
}

Paging ObjectDataSource

My ASP page code:
<asp:ObjectDataSource runat="server" ID="odsResults" OnSelecting="odsResults_Selecting" />
<tr><td>
<wssawc:SPGridViewPager ID="sgvpPagerTop" runat="server" GridViewId="sgvConversionResults" />
</td></tr>
<tr>
<td colspan="2" class="ms-vb">
<wssawc:SPGridView
runat="server"
ID="sgvConversionResults"
AutoGenerateColumns="false"
RowStyle-CssClass=""
AlternatingRowStyle-CssClass="ms-alternating"
/>
</td>
</tr>
Class code:
public partial class Convert : System.Web.UI.Page
{
...
private DataTable resultDataSource = new DataTable();
...
protected void Page_Init(object sender, EventArgs e)
{
...
resultDataSource.Columns.Add("Column1");
resultDataSource.Columns.Add("Column2");
resultDataSource.Columns.Add("Column3");
resultDataSource.Columns.Add("Column4");
...
odsResults.TypeName = GetType().AssemblyQualifiedName;
odsResults.SelectMethod = "SelectData";
odsResults.SelectCountMethod = "GetRecordCount";
odsResults.EnablePaging = true;
sgvConversionResults.DataSourceID = odsResults.ID;
ConversionResultsCreateColumns();
sgvConversionResults.AllowPaging = true;
...
}
protected void btnBTN_Click(object sender, EventArgs e)
{
// add rows into resultDataSource
}
public DataTable SelectData(DataTable ds,int startRowIndex,int maximumRows)
{
DataTable dt = new DataTable();
dt.Columns.Add("Column1");
dt.Columns.Add("Column2");
dt.Columns.Add("Column3");
dt.Columns.Add("Column4");
for (int i =startRowIndex; i<startRowIndex+10 ;i++)
{
if (i<ds.Rows.Count)
{
dt.Rows.Add(ds.Rows[i][0].ToString(), ds.Rows[i][1].ToString(),
ds.Rows[i][2].ToString(), ds.Rows[i][3].ToString());
}
}
return dt;
}
public int GetRecordCount(DataTable ds)
{
return ds.Rows.Count;
}
protected void odsResults_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["ds"] = resultDataSource;
}
}
On clicking the button resultDataSource receives some rows. Page reload and we can see result in sgvConversionResults. First 10 rows. But after click next page in pager we have message "There are no items to show in this view". When I try debug I find that after postBack page (on click next page) input params "ds" is blank, ds.Rows.Count = 0 and etc... As though resultDataSource became empty((
What did I do incorrectly?
onPostBack all variables get default values, sgvConversionResults save her structure but has clear rows. How I can save sgvConversionResults data onPostBack event???
Maybe try:
protected void Page_Load(object sender, EventArgs e)
{
sgvConversionResults.DataBind();
}
Your code seems a bit convoluted though, any particular reason you are using an ObjectDataSource? Cause you can bind the datatable directly to the gridview in the codebehind
I transmit resultDataSource with ViewState and this work success!

Resources