How to Add an Empty, Unbound, Comment Column to a GridView? - sql

I've put together a GridView table in ASP.net that allows users to approve a series of records. As part of this approval process, I'd like to provide an empty column for the user to provide comments where necessary. The only records showing up will be records that haven't been approved yet, thus, no comments will need to be loaded. Once they are approved, users will not be able to view these records again.
How do I go about adding this empty column to my GridView? Again, there's no data to load into this column. Once approved, the records in the SQL Server table will be updated with a timestamp and comments.
Most of what I was able to find on here so far has been related to adding empty rows, but not columns. I'm completely new to ASP.net, so any help would be greatly appreciated.
Current Column Code:
asp:TemplateField HeaderText="Comment" ItemStyle-Width="500px" ItemStyle-Wrap="true" SortExpression="Comment" /

Just a simple solution I've quickly put together. It uses the OnRowCommand event of the GridView.
<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField HeaderText="TextBox">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine" Text='<%# Eval("textfield") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="UpdateButton">
<ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="Update" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code behind
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
//cast the sender back to a gridview
GridView gv = sender as GridView;
//cast the commandsource back to a button
Button btn = e.CommandSource as Button;
//cast the namingcontainer of the button back to a gridviewrow
GridViewRow row = btn.NamingContainer as GridViewRow;
//find the correct textbox using findcontrol and the index obtained from the row
TextBox tb = gv.Rows[row.DataItemIndex].FindControl("TextBox1") as TextBox;
//show result
Label1.Text = tb.Text;
}
UPDATE
Or if you want to update all the records at once by pressing a button.
protected void Button1_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
TextBox tb = row.FindControl("TextBox1") as TextBox;
Label1.Text += tb.Text + "<br>";
}
}

Related

Find Radiobutton Control from ID String in vb.net

I have 3 radiobuttons in my aspx:
<asp:RadioButton ID="rdoMasculin"
runat="server" AutoPostBack="true"
Checked="true" GroupName="gender"
TextAlign="Right" />
<asp:RadioButton ID="rdoFeminin"
runat="server" AutoPostBack="true"
GroupName="gender" />
<asp:RadioButton ID="rdoAnonymous"
runat="server" AutoPostBack="true"
GroupName="gender" />
When sending the form I'm storing the ID of the checked radiobutton in a Session variable:
for example: Session("currentGender") = rdoZukuTermine.ID
When I'm getting back on the page in a later stage I'm using the following code in codebehind:
Dim currentRadio = CType(FindControl(Session("currentGender")), RadioButton)
currentRadio.Checked = True
Session.Remove("currentGender")
currentRadio ends up as a null reference. But when I'm checking the Session it contains the right ID as a string.
Can someone help me?
Is the RadioButton control directly in your .aspx or does it have any parent controls? if yes call Findcontrol() on it direct parent like this:
Dim currentRadio = CType(ParentControl.FindControl(Session("currentGender")), RadioButton)

Multiple Outputs From One Set of Labels

I am building a website in ASP.NET Web Forms with a SQL Database. The database contains a table with 571 entry's of data.
Output
Source Code
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (SqlCommand command = new SqlCommand("SELECT Id, Name, Townland, Near, Status, Built FROM Houses ORDER BY Name DESC", connection))
{
connection.Open();
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
lblId.Text = reader[0].ToString();
lblName.Text = reader[1].ToString();
lblTown.Text = reader[2].ToString();
lblNear.Text = reader[3].ToString();
lblStatus.Text = reader[4].ToString();
lblBuilt.Text = reader[5].ToString();
}
}
}
}
Database
I want to display all 571 houses to do this I would need a set of labels for each house but is there a way I could have one set of labels like the above code to output all 571 entry's without repeating code.
Try to use a Repeater. This is a special control, which allows to define an inner item layout only once and then bind some data to render multiple blocks looking consistently, but containing the different data.
Repeater has ItemTempalte property, which allows you to wrap all the labels you want to see.
<asp:Repeater ID="rptData" runat="server">
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("Id") %>'></asp:Label>
<asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
</ItemTemplate>
</asp:Repeater>
Then just load the DB rows into repeater:
rptData.DataSource = command.ExecuteReader();
rptData.DataBind();
And finally, in the HTML layout specify mapping for labels to respective columns - like
<asp:Label runat="server" Text='<%# Eval("Name") %>'></asp:Label>
or
<asp:Label runat="server" Text='<%# Eval("Townland") %>'></asp:Label>
etc.

How to get gridview clickable cell data value to new page aspx

I have two columns in a GridView where every cell of 1 column is hyperlinked.
<asp:GridView ID="gvBlogList" runat="server" DataKeyNames="Id"
AutoGenerateColumns="False" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:HyperLinkField DataTextField="id"
HeaderText="Blog id" NavigateUrl="detailspage.aspx" />
</ItemTemplate>
<ItemStyle Width="150px"></ItemStyle>
</asp:TemplateField>
</Columns>
</asp:GridView>
How can I get cell value in 2nd page as a session variable?
Thanks!
As per the what I understand from your question, you want to catch the value of the cell that is clicked, store it in session and make it accessible on the page that have redirect to. This is possible and you can try a flow like this :
You can use the GridView's inbuilt method onRowCommand in this case to generate a server side event and you can then identify which cell was clicked and lastly, you can get its value by index. The below code is a demonstration that you can try and manipulate as per your requirement. This is a sample where a Linkbutton is used in Gridview and on the click of it, its value is fetched.
You can also try to use the CommandName and CommandArgument properties if you want to assign that particular control a value that you may use at the server side.
protected void Grd1_RowCommand(object sender, GridViewCommandEventArgs e)
{
LinkButton lnk = (LinkButton)e.CommandSource;
string value = lnk.Text;
}
And for storing it in Session, you just have to make use of Session["yourVarName"] = value;. And this value can be accessed on the other page.
Hope this helps.

ASP.Net DropDownList Retains Value From Database - AutoPostBack

I have been stuck in this problem for along time, searched online but no solution yet solved my problem. I have a dropdownlist where it retrieved the plan names from a table in my databse based on the zip code that is entered in a text field. The problem I am running into is: when I enter a zip code, the plan names populate with no problem in the dropdownlist, then I select one of the plans, when I go further and try to enter other info in other fields which have autppostback = true, the dropdownlist does not retain its value and always select the first value, I want the dropdownlist to retain the selected value after the postback, here is my code:
step1.aspx
<p>
<asp:TextBox ID="txt_zip_code" runat="server" AutoPostBack="true"></asp:TextBox>
</p>
<p>
<asp:DropDownList ID="dd_dental_plan" runat="server" EnableViewState="true">
</asp:DropDownList>
</p>
step1.cs
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CustomerDataConnectionString"].ConnectionString))
{
using (SqlCommand sqlCmd = new SqlCommand())
{
sqlCmd.CommandText = "SELECT * FROM [dbo].[mfd_plans] WHERE zip_code = #zipCode";
sqlCmd.Parameters.AddWithValue("#zipCode", txt_zip_code.Text);
sqlCmd.Connection = conn;
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
da.Fill(dt);
dd_dental_plan.DataSource = dt;
dd_dental_plan.DataValueField = "plan_id";
dd_dental_plan.DataTextField = "plan_name";
dd_dental_plan.DataBind();
conn.Close();
dd_dental_plan.Items.Insert(0, new ListItem("Please select plan", "0"));
}
}
you should use update panel on your aspx page for preventing whole page load when autopostback occurs from another fields.
just put an asp:scriptmanager control on your page. After the put your zip code and dropdownlist into update panel and put other controls in separate update panels by which post back occurs. your code will look like this.
<asp:ScriptManager runat="server">
</asp:ScriptManager>
<div>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<p>
<asp:TextBox ID="txt_zip_code" runat="server" AutoPostBack="true"></asp:TextBox>
</p>
<p>
<asp:DropDownList ID="dd_dental_plan" runat="server" EnableViewState="true">
</asp:DropDownList>
</p>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
another controls which caused postback event
</ContentTemplate>
</asp:UpdatePanel>
another controls which don't cause postback.
</div>
Hope this will works.enter code here
Thanks
Umm I really hoped that I understand your Question correctly ..
Have you tried something like
if (!Page.IsPostBack)
{// All the code that you've written above ..}
and do that with all you're codes that you write in Page_Load ...
give it a try ...

visual studio 2010 Getting value from gridview

How do I get the value in a gridview when one hits the "Select" link in a row?
If I have a databound, I can get that value. However I am trying to highlight certain labels.The "HighlightTex" function lets me highlight some words that the user selects.
Here is a snippet: I am trying to get the value of "lblinvnum".
<asp:GridView ID="gvDetails" runat="server" AutoGenerateColumns="False" AllowPaging="True"
AllowSorting="True" DataSourceID="SqlDataSource1" Width="540px"
CssClass="Gridview" >
<HeaderStyle BackColor="#df5015" />
<Columns>
<asp:TemplateField HeaderText="Invnum" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:Label ID="lblInvnum" Text='<%# HighlightText(Eval("Invnum").ToString()) %>' runat="server" />
</ItemTemplate>
...
...
... For the "AmtPaid" listed below, I can Get that value. I cannot figure out how to get the value for "lblInvnum" to the snippet listed above.
<asp:BoundField DataField="AmtPaid" HeaderText="AmtPaid"
SortExpression="AmtPaid" DataFormatString="{0:c}" >
In visual basic, this is how I can get the one above for AmtPaid.
Dim index As Integer = Convert.ToInt32(e.CommandArgument)
Dim selectedRow As GridViewRow = gvDetails.Rows(index)
Dim contactCell As TableCell = selectedRow.Cells(1)
Dim contact As String = contactCell.Text
Suggestions?