How to add textbox to each and every cell in a table row and let user add multiple rows? - asp.net

Following is my markup code:
<asp:Table ID="tbldata" runat="server" BorderWidth="2">
<asp:TableHeaderRow ID="tableheaderrow1" runat="server">
<asp:TableHeaderCell Width="80" runat="server">SNO</asp:TableHeaderCell>
<asp:TableHeaderCell Width="200" runat="server">Particulars</asp:TableHeaderCell>
<asp:TableHeaderCell Width="80" runat="server">Amount</asp:TableHeaderCell>
<asp:TableHeaderCell Width="120" runat="server">Tax</asp:TableHeaderCell>
<asp:TableHeaderCell Width="300" runat="server">OtherExpenditures</asp:TableHeaderCell>
<asp:TableHeaderCell Width="80" runat="server">NetAmount</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
and following is my CS code
protected void txtCalendar_TextChanged(object sender, EventArgs e)
{
TextBox t = new TextBox();
TableRow rowNew = new TableRow(); // Creating a new table row
for (int i = 0; i < 6; i++)
{
TableCell tcell = new TableCell();
tcell.Controls.Add(t); // add the textbox control at cell zero
// or you can add it as
rowNew.Controls.Add(tcell);
}
tbldata.Controls.Add(rowNew);
}
I want to add textbox to every cell in a table row so that the user may enter data there, but with the above code, textbox is only added to the last cell. What should I do to add textbox to each and every cell? There are 5 header columns as indicated by markup above. I want to add textbox to each and every column.How can I do it? Also I want to include a button named Add Row which when clicked lets user add rows. How can I do this as well?

That is not a very easy requirement to satisfy.
Luckily Matt Dotson made a BulkEditGridView many years back.
You can download the latest code of this custom control from codeplex.

Related

Getting ASP Table from code behind does not return dynamic data

In my code asp.net page, I have an ASP table, which has a table header. In the codebehind, I generate all of the table rows. I then have a button that I want to export the data, but when I retrieve the ASP Table, it only has the table header and not the dynamically created table rows. Here is what I have
<asp:Table ID="Table1" runat="server" CssClass="display table table-hover">
<asp:TableHeaderRow ID="Table1HeaderRow"
BackColor="LightBlue"
runat="server" >
<asp:TableHeaderCell
Scope="Column" Visible="true"
Text="ID" ID="columnID" />
<asp:TableHeaderCell
Scope="Column" Visible="true"
Text="Name" ID="columnName" />
</asp:TableHeaderRow>
</asp:Table>
I then build by data rows like this:
Dim TR As New TableRow
Dim id_cell As New TableCell
Dim title_cell As New TableCell
id_cell.Controls.Add(New LiteralControl(item.ID))
title_cell.Controls.Add(New LiteralControl(item.name))
TR.Cells.Add(id_cell)
TR.Cells.Add(title_cell)
Me.Table1.Rows.Add(TR)
I also have a button on the page, but when I retrieve the Table, it just has the header rows, and not the dynamic rows:
Dim table As Table = Me.Table1
Dim count as integer = table.rows.count 'this just has one
How do I get then entire table? As a side note, I have to use the Table control, as opposed to gridview or repeater, for other reasons.

how to change the position of the added itemtemplate in gridview in asp.net

I have a gridview with a stored proc datasource
when i try to fill out the datagrid, i cannot control the position of the columns, when i add an additional item template, it is automatically positioned on the right but i want it to be on the left side. what should i do?
i want the edit linkbuttons to be placed on the left after the date total
Gridview Code:
<asp:GridView ID="ListView1" runat="server" DataSourceID="dsSched">
<Columns>
<asp:TemplateField>
<ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server">Edit</asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server">Cancel</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
i've got this right by using the rowdatabound event
protected void ListView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
// Retrieve row
GridViewRow row = e.Row;
// Intitialize TableCell list
List cells = new List();
foreach (DataControlField column in ListView1.Columns)
{
// Retrieve first cell
TableCell cell = row.Cells[0];
// Remove cell
row.Cells.Remove(cell);
// Add cell to list
cells.Add(cell);
}
// Add cells
row.Cells.AddRange(cells.ToArray());
}
You can use
GridView.RowStyle property and set :horizontal-align to left.

select data from gridview field and populate textbox

I'm trying to populate a single text box (or parameter) with data from a gridview column when I click on a button in that row.
Gridview gets it data from a sqlconnection
the gridview is
| Drawing |
| 12345 | VIEW
| 12346 | VIEW
the VIEW is a template button with an onclick event, when the user clicks the button the data from the Drawing column (12345) should be passed to ether a textbox or a paremeter. (this is the part I dont know how to do) once the Iv got the number in a textbox I can use it as pareameter and then a pdf is opened of that drawing, I have code for this and is working.
thanks for any help
If you are using C#, the simplest thing to do would be to add an in-built select command button to the gridview rows at runtime. Then on the selectedindexchanged event of the gridview simply access the cell of the selected row that you want the value from. You can then assign that string to anything you want. Like so:
protected void myGridView_SelectedIndexChanged(object sender, EventArgs e)
{
string myString = myGridView.SelectedRow.Cells[4].Text.ToString();
TextBox1.Text = myString;
}
Remember that the cell index collection is zero based, so [0] is actually the first cell in the row.
Use TemplateFields and the grid view's OnRowCommand event, like this:
Markup:
<asp:gridview id="GridView1"
OnRowCommand="GridView1_RowCommand"
runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="TextBoxDrawing" runat="server"
Text="<%# Eval("Drawing")) %>" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="selc" runat="server" Text="View"
CommandName="View"
CommandArgument="<%# ((GridViewRow)Container).RowIndex %> />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code-behind:
protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e)
{
// If multiple buttons are used in a GridView control, use the
// CommandName property to determine which button was clicked
if(e.CommandName == "View")
{
// Convert the row index stored in the CommandArgument
// property to an integer
var index = Convert.ToInt32(e.CommandArgument);
// Retrieve the row that contains the button clicked
// by the user from the Rows collection
var row = GridView1.Rows[index];
// Find the drawing value
var theDrawingTextBox = row.FindControl("TextBoxDrawing") as TextBox;
// Verify the text box exists before we try to use it
if(theDrawingTextBox != null)
{
var theDrawingValue = theDrawingTextBox.Text;
// Do something here with drawing value
}
}
}

Add rows to a table dynamically

At the moment I am trying to create a table whose content is supposed to be created by a subclass (result of querying a RESTful web service). I have been working for quite some time on that now and I just cannot seem to get it to work. I have tried so many different solutions.
creating the table in the subclass and add it to Page.Controls. That gets me "The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>)." which does not make sense at all.
I have tried to create an empty table on the page and passed its handle to the subclass which was responsible for adding rows. Noting happened.
I have tried to return a TableRowCollection and assigned it to the previously created (empty) table. Nothing happened.
Now I just want to add one row and one cell to the table (baby steps towards what I need). Not even that works. Please find the code for that attached:
TableRow row = new TableRow();
table.Rows.Add(row);
TableCell cell = new TableCell();
row.Cells.Add(cell);
cell.Controls.Add(new TextBox());
The table is simple and empty:
<asp:Table ID="table" runat="server" />
The source code that is displayed by my browser looks like that:
<table id="table">
</table>
I have been looking at countless examples on the web and all look like this. I guess it is only a tiny problem somewhere but I have not been able to figure it out. Now I am willing to offer life-long gratitude to everybody who can provide the hint for solving this mess.
It is working, I have tested it:
In my page load, I have:
protected void Page_Load(object sender, EventArgs e)
{
TableRow row = new TableRow();
TableCell cell = new TableCell();
cell.Controls.Add(new TextBox());
row.Cells.Add(cell);
table.Rows.Add(row);
}
On my aspx page, I have:
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<asp:Table ID="table" runat="server" />
</asp:Content>
The html rendered by this code:
<table id="MainContent_table">
<tbody><tr>
<td><input name="ctl00$MainContent$ctl00" type="text"></td>
</tr>
</tbody></table>
I would use either an asp:GridView or an asp:Repeater
eg with Repeater
<table>
<asp:Repeater id="repeater1" runat="server">
<ItemTemplate>
<tr>
<td><asp:Literal id="literal1" runat="server" /></td>
</tr>
</ItemTemplate>
</asp:Repeater>
</table>
then in your code behind
repeater1.DataSource = myDatasource;
repeater1.DataBind();
or you could use a GridView
using table.Rows.Add() will tend to cause you problems, particularly with disappearing content on postback, or problems with event handlers not firing should you need to add any LinkButtons or anything like that to your table cells
The question is where are you adding these rows in the table, I mean in which event of page?
As I feel that post back is clearing all the added rows.
Kindly tell the execution sequence and also try to put your code in page_init event for adding rows.
Might solve your problem.
Make table runat="server" in your aspx code
<table id="tbl" runat="server">
</table>
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TableRow tr = new TableRow();
TableCell tc = new TableCell();
TextBox txtBox = new TextBox();
// Add the control to the TableCell
tc.Controls.Add(txtBox);
// Add the TableCell to the TableRow
tr.Cells.Add(tc);
// Add the TableRow to the Table
tbl.Rows.Add(tr);
}
}

Cant get value from textbox on gridview

I have a GridView on an ASP.NET page with a TemplateField column that has a TextBox in the ItemTemplate. I then have a command field which is supposed to pull the text from this TextBox and use it in a SqlCommand running a stored procedure.
Here is my C# code:
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow selectedRow = this.gvwSNConfirm.Rows[index];
TableCell intID = selectedRow.Cells[1];
int reqID = int.Parse(intID.Text);
// Get the SN from the text box on the selected row
TableCell snCell = selectedRow.Cells[0];
TextBox tbox = (TextBox)staffNum.FindControl("txtSN");
string stSN = tbox.Text.ToString();
When I add a break point to see the values for intID and tbox.Text I get the right ID but the Text for tbox is "".
The mark-up for the two columns I am referencing is
<asp:TemplateField HeaderText="SN">
<ItemTemplate>
<asp:TextBox ID="txtSN" runat="server" MaxLength="20" Width="100px" />
</ItemTemplate>
<HeaderStyle BackColor="#171695" Font-Names="Arial" Font-Size="Small" ForeColor="White" Wrap="true" />
</asp:TemplateField>
<asp:BoundField DataField="Request" HeaderText="Request" ReadOnly="true" SortExpression="Request" />
Can anyone provide any help why I cant get the text from the text box? It worked the first time round but subsequently all tbox text values have been "".
Many Thanks
Andy
NEW CODE (05/03/2010):
protected void gvwSecond_RowCommand(object sender, GridViewCommandEventArgs e)
{
if ((e.CommandName == "Confirm") || (e.CommandName == "Decline"))
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvwSecond.Rows[index];
int secondID = int.Parse(row.Cells[1].Text);
TextBox txtsn = ((TextBox)row.FindControl("txtSecSN"));
string sn = txtsn.Text;
What event is this code in? You should e.Item.FindControl. If you are getting the right control for sure, but Text is empty, that means it's either not getting posted back, or it's being cleared somewhere else.
I had the same problem once. I also had to retrieve the text value from a TextBox in a GridView on server and I was always getting an empty string. I had to set EnableViewState="false" for both the Gridview and the Textbox and it worked like a charm.

Resources