Dynamic Table: Link button to textbox cell by cell - c#

I have a dynamic table, where, on every row I have a text box (txtCantitate) and a button (btnMinus). In the textbox I have quantity (int) and on the button click I want the quantity to decrease by one. Here you have what I have on the table:
Can you help me make the code for the buttons? The problem is that it is a dynamic button... on every record it has the same ID... I don't know how to do it...
My languages used on the project C#, .NET 4.5, js, Jquery.
cell = new HtmlTableCell();
HtmlInputButton btnMinus = new HtmlInputButton();
btnMinus.ID = "btnMinus";
btnMinus.Value = "-";
cell.Controls.Add(btnMinus);
row.Cells.Add(cell);
cell = new HtmlTableCell();
HtmlInputText txtCantitate = new HtmlInputText();
txtCantitate.ID = "txtCantitate";
txtCantitate.Value = publicatie.Cantitate.ToString();
cell.Controls.Add(txtCantitate);
row.Cells.Add(cell);

You need to set a click event on the button, which will perform the action you want:
You will need to set the ID of both the textbox and the button to match the row+cell index you're in first... since these are HtmlControls, you don't really have their index so you'll have to find a way to get these in there somehow (i won't code this for you, sorry).
btnMinus.ID = "btnMinus_" + CurrentRowIndex.ToString() + "_" + CurrentCellIndex.ToString();
txtCantitate.ID = "txtCantitate_" + CurrentRowIndex.ToString() + "_" + CurrentCellIndex.ToString();
then you will have to set the event handler...
server side click event handler setter (see below for actual event handler code):
btnMinus.Click += myButtonClick;
client-side click event handler setter:
btnMinus.Attributes.Add("onclick","JavaScript:myButtonClick(this);");
If you want to do the event handler code server-side:
private void myButtonClick(object sender, EventArgs e)
{
Button tmp = sender as Button;
string[] id = tmp.ID.Split(new string[]{"_"}, StringSplitOptions.None);
string textbox_ID = "txtCantitate" + "_" + id[1] + "_" + id[2];
TextBox txt = this.Controls.FindControl(textbox_ID) as TextBox;
int val = -1;
string finaltext = "";
if(int.TryParse(txt.Text, out val))
finaltext = (val-1).ToString();
else
finaltext = "Invalid number, Cannot decrement!";
txt.Text = finaltext;
}
If you want to do the event handler code client-side:
function myButtonClick(object sender)
{
//i'll let you figure this one out for yourself if you want to do it client-side, but it's very similar to the server-side one as far as logic is concerned...
}

Here is the solution,
Javascript
function MinusVal(ctrl)
{
var TextBox = $(ctrl).parent().next().find("input[type=text]");
var Value = parseInt(TextBox.val());
TextBox.val(Value - 1);
return false;
}
C# Backend
btnMinus.Attributes.Add("onclick", "MinusVal(this);");

Related

c# equivalent to document.getElementById("ListBox"+i+"Item"+j).selected=true

I've created a dynamic webpage using strictly html, javascript, and MS Access. While it was functional, locally, there were complications deploying it. Since I have ported the data to MySQL and am trying to use Visual Studio's aspx.cs to do much of what the javascript did previously.
I have a screen that populates a dynamic set of rows based on a query result (two rows per record for aesthetics), one of the cells contains a drop down menu(html select/ asp:ListBox).
When I had everything only on javascript, I could create the cell, then create its contents, then set the selected value using:
document.getElementById("StatusDD" + rowCount).value = reader.GetValue(i);
From what I've gathered so far, the rough equivalent is:
ListItem li = StatusDD1.Items.FindByValue(reader.GetValue(i));
li.Selected = true;
However, I cannot simply hardcode StatusDD1 thru StatusDDx (for one, at the beginning my hardcoded set might be larger than the number of records returned, and two eventually the rows returned will be larger than the set of hardcoded values).
So what I did was I created the following function:
protected void setSelected(string selectId, string value)
{
/*Need to put something here to make the following work*/
selectId.Items.FindByValue(value).Selected = true;
}
The selectId being passed in is the name/id of the ListBox and the value is the value coming back from the query.
It's called like:
setSelected("StatusDD" + rowCount, (string)reader.GetValue(i));
If I could, for lack of better phrase, materialize the name created by "StatusDD"+rowCount, I could pass that name in as if I was passing in a ListBox, rather than a string.
Alternatively, if there was a way to select the ListBox from an array where I could do a conditional check WHERE/IF ListBox.Name = selectId, something like the following PseudoCode:
ListBox a = ListBox.NameMatches(selectId);
a.Items.FindByValue(value).Selected = true;
Currently ListBoxes are being created by defining the box in a string and then passing that string into an HtmlTableCell:
HtmlTable myTable = new HtmlTable();
HtmlTableRow newRow;
string cellId;
string cellContents;
int rowCount = 1;
string statusDisabled = "";
while (reader.Read()){
newRow = new HtmlTableRow();
myTable.Rows.Add( newRow );
...
...
cellContents = "<asp:ListBox name='StatusDD" + rowCount + "' id='StatusDD" + rowCount + "' style='width:100%; " + statusDisabled + "' value='" + reader.GetValue(i) + "' onchange='markNeedSave(" + (rowCount + 1) + ")'><asp:ListItem value='0'></asp:ListItem><asp:ListItem value='1'>New</asp:ListItem>....asp:ListBox>";
newRow.Cells.Add(new HtmlTableCell{InnerHtml = cellContents});
}
If it helps, here's how I had it working in javascript:
while (!rs.EOF) {
rowa = table.insertRow(rowCount);
rowa.id = "RECORD" + rowCount + "a";
cell = rowa.insertCell(i + 1);
cell.id = "RECORD" + rowCount + "_CELL" + (i + 1);
for (i = 0; i < 8; i++) {
cell.innerHTML = "<select name='StatusDD" + rowCount + "' id='StatusDD" + rowCount + "' style='width:100%' value='" + rs.fields(i).value + "' onchange='markNeedSave(" + (rowCount + 1) + ")'><option value='NONE'></option><option value='New'>New</option>...</select>";
if (readonly) {
document.getElementById("StatusDD" + rowCount).disabled = true;
}
document.getElementById("StatusDD" + rowCount).value = rs.fields(i).value;
}
...
}
OK, got the ListBox to work, but as I was researching, and when I finally got it to work, I discovered that what I wanted was the DropDownList, not the ListBox, but the same fixes needed to be done in order to get either to work.
I use the following function now:
protected void setSelected(string selectId, string value)
{
PlaceHolder TCS = Page.FindControl("TestingCS") as PlaceHolder;
DropDownList ddl = TCS.FindControl(selectId) as DropDownList;
if (ddl != null)
{
ddl.SelectedValue = value;
ListItem item = ddl.Items.FindByValue(value);
if(item != null)
{ item.Selected = true;}
}
}
Also, for my cell contents that just contain data using the following is fine:
cellContents = "<someString>";
newRow.Cells.Add(new HtmlTableCell{InnerHtml = cellContents});
but for my drop down (or list box) I need to use:
cell = new HtmlTableCell();
newRow.Cells.Add(cell);
DropList = new DropDownList();
DropList.ID = "StatusDD" + rowCount;
DropList.Items.Add(new ListItem("", "0"));
DropList.Items.Add(new ListItem("New", "1"));
...
cell.Controls.Add(DropList);
setSelected(DropList.ID, (string)(reader.GetValue(i)));
A smoother solution:
protected void setSelected(DropDownList ddl, string value)
{
ListItem item = ddl.Items.FindByValue(value);
if (item != null)
{ item.Selected = true; }
}
...
protected void accessRecord()
{
...
DropList = new DropDownList();
DropList.ID = "StatusDD" + rowCount;
DropList.Attributes["onChange"] = "javascript:markNeedSave(" + rowCount + ");";
DropList.Items.Add(new ListItem("", "0"));
DropList.Items.Add(new ListItem("New", "1"));
...
cell.Controls.Add(DropList);
setSelected(DropList,(string)reader.GetValue(i));
}
...
It sounds like the function you're looking for is FindControl. This can be used from the Page, or any parent control you might have created to hold your output.
An example implementation of your setSelected method might look like this:
protected void SetSelected(string selectId, string value)
{
var lb = Page.FindControl(selectId) as ListBox;
if (lb != null)
{
var item = lb.Items.FindByValue(value)
if(item != null)
item.Selected = true;
}
}

Using List content for filtering datagridview C#

I've been struggling with this question for a few days now and I haven't found any answer yet; I created a ToolStripMenu array which is filled dynamically from a stored procedure:
ToolStripMenuItem[] itemsDepto = null;
itemsDepto = new ToolStripMenuItem[data.Tables[0].Rows.Count];
for (int i = 0; i <= data.Tables[0].Rows.Count - 1; i++)
{
itemsDepto[i] = new ToolStripMenuItem();
itemsDepto[i].Tag = data.Tables[0].Rows[i].ItemArray[0];
itemsDepto[i].Text = data.Tables[0].Rows[i].ItemArray[1].ToString();
itemsDepto[i].CheckOnClick = true;
itemsDepto[i].Checked = true;
itemsDepto[i].Click += DeptoFilter_Click;
deptoList.Add(data.Tables[0].Rows[i].ItemArray[1].ToString());
}
tsmiDepartamento.DropDownItems.AddRange(itemsDepto);
And what I'm trying to achieve is use this ToolStripMenu as a filter control for the user, by default is Checked so when the user unchecks the menu, it should filter the rows with the content that is unchecked.
In the click event I add and remove values from the list depending on the state of the menu button as you can see in the following example:
private void DeptoFilter_Click(object sender, EventArgs e)
{
ToolStripMenuItem temp = new ToolStripMenuItem();
temp = (ToolStripMenuItem)sender;
BindingSource bind = new BindingSource();
bind.DataSource = dgvPersonalTotal.DataSource;
if (temp.CheckState == CheckState.Checked)
{
deptoList.Add(sender.ToString());
}
else
{
deptoList.Remove(sender.ToString());
}
bind.Filter = "Departamento NOT IN (" + /*LIST*/"" + ")";
dgvPersonalTotal.DataSource = bind;
//foreach (string x in deptoList)
//{
//}
}
But the big question I have is, how can I use a list to filter the Binding Source, as you can see in the code, I can't just use the list or even try use the BindingSource.Filter in the foreach, I don't know how to resolve this problem, so any idea is well appreciated.
bind.Filter = "Departamento NOT IN (" + string.Join(",", deptoList.ToArray()) + ")";

Add Ajax CalendarExtender to a dynamic textBox in ASP.NET C#

Is there a way to add Ajax CalendarExtender to a dynamic ASP.NET textbox control? Basically I'm trying to do the following:
protected void Page_Load(object sender, EventArgs e)
{
database.DB myDB = new database.DB();
DataTable myVars = new DataTable();
string myTopicID = (string)Session["myTopicID"];
bool myInvite = (bool)Session["myInvite"];
bool mySig = (bool)Session["mySig"];
string myLogo = (string)Session["myLogo"];
string myImage = (string)Session["myImage"];
string myLanguage = (string)Session["myLanguage"];
myVars = myDB.getVarFields(myTopicID, myLanguage);
AjaxControlToolkit.CalendarExtender calenderDate = new AjaxControlToolkit.CalendarExtender();
for (int i = 0; i < myVars.Rows.Count; i++)
{
Label label = new Label();
TextBox text = new TextBox();
label.Text = Convert.ToString(myVars.Rows[i]["varName"]);
myPlaceHolder.Controls.Add(label);
text.ID = Convert.ToString(myVars.Rows[i]["varName"]);
myPlaceHolder.Controls.Add(new LiteralControl(" "));
myPlaceHolder.Controls.Add(text);
if (Convert.ToString(myVars.Rows[i]["varName"]).Contains("Date:"))
{
calenderDate.TargetControlID = "ContentPlaceHolder1_" + text.ID;
myPlaceHolder.Controls.Add(calenderDate);
}
myPlaceHolder.Controls.Add(new LiteralControl("<br />"));
}
}
The error I get when I run the code above is the following:
The TargetControlID of '' is not valid. A control with ID 'ContentPlaceHolder1_Date:' could not be found.
Which makes sense I suppose since the actual text box does not exist yet. But is there a way around this?
I think ASP.NET will be smart enough to handle it if you just use text.ID, you shouldn't need to add the ContentPlaceHolder1_ prefix.
If that doesn't work, you can use the TextBox' ClientIdMode property to set it to static, then text.ID will definitely work.
The following code worked locally for me:
AjaxControlToolkit.CalendarExtender calenderDate = new AjaxControlToolkit.CalendarExtender();
for (int i = 0; i < 2; i++)
{
Label label = new Label();
TextBox text = new TextBox();
label.Text = Convert.ToString("varName");
ph1.Controls.Add(label);
text.ID = "myId" + i;
ph1.Controls.Add(new LiteralControl(" "));
ph1.Controls.Add(text);
calenderDate.TargetControlID = text.ID;
ph1.Controls.Add(calenderDate);
ph1.Controls.Add(new LiteralControl("<br />"));
}
Only differences I think you may want to investigate: I'm using latest ControlToolkit from Nuget, I'm using a ToolkitScriptManager instead of default ScriptManager. One thing that may be important to you is making sure you make text.ID unique.

Dynamic C# Not rendering on page_load

I have a method which is intended to dynamically generate a series of divs based on the entry of a value from a dropdown list. However, I wish to reuse the same code to generate the tables on the first page_load when a number already exists.
This is where the method is called. It is called GenerateTables and it is called from the Page_Load event:
if (!IsPostBack)
{
AcademicProgramme programme;
if (Request.QueryString["id"] != null)
{
programme = academic.GetAcademicProgramme(Request.QueryString["id"]);
programmeName.Text = programme.Name;
PopulateView(programme);
GenerateTables(programme.Levels);
}
}
And here is the method itself (apologies for the size of the method):
private void GenerateTables(int count)
{
for (int i = 1; i < count + 1; i++)
{
LiteralControl title = new LiteralControl();
LiteralControl close = new LiteralControl();
LiteralControl close2 = new LiteralControl();
String script = "<div class=\"ModuleProgTable\"><h3>Level " + i + "</Modules></h3></br>";
title.Text = script;
AcademicTable.Controls.Add(title);
Panel panel = new Panel();
panel.ID = "Level" + i + "Modules";
PopulatePanel(panel, GetModulesSession(i));
Button a = new Button();
a.ID = "AddModule" + i;
a.Text = "Add Module";
a.Click += (OpenPopup);
AcademicTable.Controls.Add(panel);
AcademicTable.Controls.Add(a);
close.Text = "</div> <!-- Close here -->";
close2.Text = "</div>";
AcademicTable.Controls.Add(close);
}
}
The divs are clearly being populated because if I change the dropdown option then they appear on the PostBack without fail. It's when I try to get them to render on the first page_load that I am having problems.
Any feedback and advice would be greatly appreciated!
Regards,
-Michael

Click Event for Dynamically Generate Linkbutton

i created a simple registration page which have fields Name,Username and Password.. actually am checking the username with database whether that username already had or not...
suppose if that username already taken,then i display"Username Already Taken" message in label,and also i create dynamically 4 link buttons for username suggestions like
now when i click on linkbuttons,that are bind to username textbox.. here is my code
give me some idea how to bind that linkbutton to textbox..
You should minimize server requests from client, especially if you want to update UI controls. I suggest to use java script to update text box with new value.
for (int i = 0; i < 4; i++)
{
LinkButton lbtn = new LinkButton();
lbtn.OnClientClick = "document.getElementById('" + txtuname.ClientID + "').value = '"+txtuname.Text + i+"'; return false;";
lbtn.Text = txtuname.Text + i;
phlinks.Controls.Add(lbtn);
phlinks.Controls.Add(new LiteralControl(" "));
}
Try this
for (int i = 0; i < 4; i++)
{
LinkButton lbtn = new LinkButton();
lbtn.OnClientClick = "document.getElementById('" + txtuname.ClientID + "').value = this.innerText; return false;";
lbtn.Text = i.ToString();
phlinks.Controls.Add(lbtn);
}

Categories