Gridview with TextBox - asp.net

i have an gridview control with a comment text, link button, and an
invisible (text box and a button to post to database.)
when i click on the link button i want to show the textbox.
can any one help me how to do this.
my gridview code:
<asp:GridView ID="grdComments" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table width="500px" cellpadding="3" cellspacing="3">
<tr/>
<td/>
<asp:Label runat="server" ID="lblLeftPad"></asp:Label>
<asp:Label runat="server" ID="lblComment" Text='<%# container.dataitem("CommentText") %>'></asp:Label>
</td>
</tr>
<tr>
<td>
<asp:LinkButton ID="lbtnReply" Text="Reply" runat="server" CommandName="CommentReply"></asp:LinkButton>
</td>
</tr>
<tr>
<td>
</asp:TextBox ID="txtReply" runat="server" Height="50px" Width="500px" Visible="false"></asp:TextBox>
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>

If you are not using Javascript / AJAX then on the link button's click event set the textbox's visible value to true. The link button automatically sends a postback so this would work unless you have the link button set to not auto postback.
EDIT: To access link button
There are a few ways depending on how you setup your grid. If this is a Command Field or a button field that you are using then you can use the RowCommand and the e.CommandArgument to now which row you are on and then set the textbox to true. Below is a sample:
row = Integer.Parse(e.CommandArgument)
GridView1.Rows(row).Cells(1).Controls(1).Visible = True
The cell is set to the column you are wanting to work on and the controls # is set to the control you want to work with in the cell. There is more than one control created in a cell even if you only put a textbox. You can use the FindControl syntax to more reliably get at your control.
If you created a templated field with the link button then on the command argument for the link button set it's value to: =<%# CType(Container,GridViewRow).RowIndex %>
and the above code in the gridview's rowcommand will work.
OR you can set the link buttons click event to something like:
gridview1.rows(directcast(sender,LinkButton).CommandArgument).cells(1).Controls(1).visible = true
You can get to the link buttons click event in a templated field by editing the template from the GUI and double clicking on the link button.
I would recommend using the RowCommand option and using the FindControl Syntax to make your app more readable and easier to maintain.

Related

Is the TextChanged Event not triggered by a TextBox in TextMode="Password"?

I am creating a password text field that shows/hides password requirements as the user types their password into a TextBox with TextMode="Password". I added a TextChanged event so I could check the TextBox text against the requirements each time a new character is typed/removed.
The problem I am having is that the TextChanged event is seemingly not being triggered ever. Here is the relevant code:
<tr>
<td style="text-align: right">
<asp:Label ID="Label1" runat="server" CssClass="BoldLabel14px" Font-Bold="True"
Text="New Password"></asp:Label>
</td>
<td>
<asp:TextBox ID="NewPassword" runat="server" Font-Names="calibri"
Font-Size="14px" TextMode="Password"></asp:TextBox>
</td>
</tr>
Protected Sub NewPassword_TextChanged(sender As Object, e As EventArgs) Handles NewPassword.TextChanged
System.Diagnostics.Debug.WriteLine("NewPassword Text = " & Me.NewPassword.Text)
CheckPasswordRequirements(Me.NewPassword.Text)
Me.PasswordRequirementsCapital.Visible = False
End Sub
From this code, the output never shows the debug statement as the TextBox is modified. Is different way to grab the text as it is written? I am using Visual Studio, vb.net, and asp.net.
The main reason the event isn't fired is because you don't have an event tied up to even be fired.
It should be something like:
<asp:TextBox ID="NewPassword" runat="server" Font-Names="calibri" Font-Size="14px" TextMode="Password" ontextchanged="NewPassword_TextChanged" AutoPostBack="True"></asp:TextBox>
The event itself will not fire until you leave though, it's a server side process and only happens between posts to the server. If you want to handle this as the text is changing look into JavaScript and or JQuery.

Using TextBox TextChanged event to enable or disable a button control in asp.net

I am using an asp TextBox control with its TextChanged event and my goal is to capture text as a user enters it. If there are one or more characters entered, I would like a button control to be enabled without the user having to leave the TextBox control.
My source code for the TextBox on the aspx page is
<asp:TextBox ID="NewSpendingCategoryTextBox" MaxLength="12" runat="server"
AutoPostBack="True"
OnTextChanged="NewSpendingCategoryTextBox_TextChanged"
ViewStateMode="Enabled" >
</asp:TextBox>
and my source code on the code behind page is
Protected Sub NewSpendingCategoryTextBox_TextChanged(sender As Object, e As System.EventArgs) Handles NewSpendingCategoryTextBox.TextChanged
Dim strSpendingCategoryTextBox As String = Nothing
strSpendingCategoryTextBox = NewSpendingCategoryTextBox.Text
If strSpendingCategoryTextBox.Length <= 0 Then
Me.NewSpendingCategoryInsertButton.Enabled = False
Else 'strSpendingCategoryTextBox.Length > 0
Me.NewSpendingCategoryInsertButton.Enabled = True
End If 'strSpendingCategoryTextBox.Length <= 0
End Sub
So it appears I have to use javascript to enable or disable the insert button. Can someone guide me on how to get an element within a table? The table sits in a Panel as well.
below is the aspx code...
<asp:Panel ID="AddSpendingCategoryPanel" Visible="false" runat="server">
<table class="AddNewTable">
<tbody>
<tr>
<td>
<asp:Label ID="lblSpend" runat="server"
Text="Spending Category:">
</asp:Label>
</td>
<td>
<asp:TextBox ID="txtSpend" MaxLength="12"
runat="server"
AutoPostBack="True"
OnTextChanged="txtSpend_TextChanged"
OnKeyDown="return CheckSpendTextBoxValue()"
ViewStateMode="Enabled" >
</asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button CssClass="frmbtn" ID="btnInsertSpend"
runat="server" Text="Insert" />
</td>
<td>
<asp:Button CssClass="frmbtn" ID="btnCancelSpend"
runat="server" Text="Cancel"
CausesValidation="False" />
</td>
</tr>
</tbody>
</table>
</asp:Panel>
Run this code in the OnKeyPress event or consider JavaScript. The textbox does not fire the Text_Changed event til Tab or Enter are used.
Simplify the boolean check.
Me.NewSpendingCategoryInsertButton.Enabled = (NewSpendingCategoryTextBox.Text.Length <> 0)
I'm not sure exactly how you would do it. But the ASP.NET code is executed on the server that is hosting the web page.
I'd highly recommended doing it on JavaScript which can be run client side. Hopefully this article is of use to you.
How to check if a textbox is empty using javascript

ListView programmatically adding row after databind

What I would like to achieve:
I would like a user to be able in insert a row into the listview. BUT not into a database.
What I am stuck on:
Currently I am stuck on the OnItemCommand, i dont seem to be entering the method. Any help would be great Code below.
<LayoutTemplate>
<table>
<th>
</th>
<th class="grayHeader">
<asp:Label ID="lblHeader" runat="server" />
</th>
<tr>
<asp:PlaceHolder runat="server" ID="itemPlaceholder"></asp:PlaceHolder>
</tr>
</table>
</LayoutTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Button runat="server" ID="btnDeletePerson" Text="-" CommandName="deletePerson"/>
</td>
<td>
<asp:Label ID="lblPerson" runat="server" Text='<% #Eval("Person") %>'></asp:Label>
</td>
</tr>
</ItemTemplate>
<InsertItemTemplate>
<tr>
<td>
<asp:Button ID="btnAddUser" runat="server" CommandName="Insert" ext="+" />
</td>
<td>
<asp:TextBox runat="server" ID="txtInsert"></asp:TextBox>
</td>
</tr>
</InsertItemTemplate>
Protected Sub ListGrantee_OnItemCommand(ByVal sender As Object, ByVal e As ListViewCommandEventArgs)
Select Case e.CommandName
Case "Insert"
Dim test As ListViewItem
test = New ListViewItem("test")
listGrantee.Items.Add(test)
Case ""
Case Else
End Select
End Sub
I would save you temporarily created Users in the ListView's Datasource flagged as temporary(add a new DataColumn). Afterwards you have to DataBind the Listview. Store the flag in an invisible control(label) so that it is saved in the ViewState on Postbacks.
Further to my comment - as I understand your problem, your OnItemCommand event handler is not getting triggered. Here's how I set up event handlers (using VS2008).
In the design view for the aspx file, I highlight the control that I am interested in. Then, in the "Properties" window, I click on the Event's button (the little lightning flash), and scroll down the list of events until I find the one I am interested in.
Double click in the column next to the event name - this will bring up the code behind page, with the shell of the event handler in place - including the all important "handles ...." clause.
Now enter your event handling code....
I had viewstate turned off. Which disabled the event mechanism, flipped view state back on problem solved.

FindControl("someTextBox") in GridView not sending the updated value

Im populating a GridView from List so am forced to use TemplateField controls to allow editing. This requires displaying a TextBox populated with the original value when in edit mode and using FindControl to get the new value out on update submit.
Problem is foundTextBox.Text == "OriginalTextBoxValue"
<asp:TemplateField HeaderText="A Field">
<ItemTemplate>
<asp:Label ID="_theLabel" runat="server" Text='<%# Eval("AField") %>' />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_theTextBox" runat="server" Text='<%# Eval("AField") %>' />
</EditItemTemplate>
</asp:TemplateField>
And the code in my update event handler
TextBox newText = (TextBox)_myGridView.Rows[e.RowIndex].FindControl("_thTextBox");
//newText.Text == the old value of the text box
Is your gridview binded at every postback? This could explain why you never get the updated value, because the gridview is rebinded before reading the textbox.
Could you paste your complete update method?
You've got the code behind in the wrong event handler. Move it to the Editing event handler, so it will populate the textbox whenever the user clicks on the Edit command for a row.

Update panel and javascript

I have a user control whose structure is -
<updatepanel UpdateMode="Always">
<ContentTemplate>
<asp:FormView>
<EditItemTemplate>
<table>
<tr id='tr1'>
<td>
Textbox1
btn1
</td>
</tr>
<tr id='tr2'>
<td>
textbox2
</td>
</tr>
<table>
</EditItemTemplate>
</asp:FormView>
</ContentTemplate>
</updatepanel>
On page load I use following to hide tr2
ClientScriptManager cs = Page.ClientScript;
csText.AppendLine("");
csText.AppendLine("if(document.getElementById('tr2')!=null)
document.getElementById('tr2').style.display = 'none';");
csText.AppendLine("");
cs.RegisterStartupScript(this, this.GetType(), csName, csText.ToString(), false);
This part is working fine. Now on click of the btn1 I am popping up the modal popup extendar control. This causes the tr2 to show again. I know that the script needs to be inline with the updatePanel. I tried using ScriptManager.RegisterStartupScript(....); but does not work. Also the updateMode needs to be always so that the data from pop extendar can be put inside the Textbox1
All the help is appreciated.
Thank you
Since you're using an UpdatePanel, you don't really need any javascript for this.
If you change your tr2 element to ...
<tr id="tr2" runat="server">
this will allow you to control it in your server side code. To hide your tr2 row, simply do...
tr2.Style["display"] = "none";
this should keep things in sync nicely on callbacks.

Resources