Getting text out of textarea from a ASP.NET GridView update - asp.net

i'm loosing my mind. Using ASP.NET in a GridView, amongst other controls, I have the following:
<asp:TemplateField HeaderText="Intention">
<EditItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="true" TextMode="MultiLine" Wrap="true" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:TextBox ID="IntentionInfo" Enabled="false" TextMode="MultiLine" runat="server" />
</ItemTemplate>
</asp:TemplateField>
I would like to take the value out of this textarea and save in a database. However, server side, I try to pull the value out, like such:
string txt = (TextBox)DonationResultsTable.Rows[e.RowIndex].Cells[6].Controls[1].Text;
... but I keep getting the value that was SENT to the Client.
I wrote this javascript and I can see the values change in the DOM, but still the server keeps taking the old value.
$("textarea").change(function()
{
var txt = $(this).val();
$(this).html(txt).text(txt);
});
So my guess was ViewState, but I disabled it for those controls, like this:
<asp:TextBox ID="IntentionInfo" ViewStateMode="Disabled" Enabled="false" TextMode="MultiLine" runat="server" />
Still nothing! Any ideas?

One option could be to use a hidden field and update it on text changed for the text area. You could do this with jQuery like this:
$("textarea[id$=tbTest]").change(function () {
$("input[id$=hdnVal]").val($("textarea[id$=tbTest]").val());
});
Then on the server side, you can retrieve the hidden field's value and save it to your database.

Related

how to get assign values to check box in grid view

i want to know status is active or not in check box. how to bind check box with database data. while editing im lossing previous data. data is not showing in check box
code behind updating event
cmd.Parameters.AddWithValue("#LeadsAccess", ChkLeads.Checked);
<asp:TemplateField HeaderText="Leads">
<ItemTemplate>
<asp:CheckBox ID="ChkLeads" runat="server" />
</ItemTemplate>
</asp:TemplateField>
db column- [Leads]
You can try like this
<asp:CheckBox ID="ChkLeads" runat="server"
Checked='<%#bool.Parse(Eval("columnName").ToString())%>' />
try this..
you can bind the db value like this..
<asp:CheckBox ID="ChkLeads" runat="server" AutoPostBack="true" Checked='<%#Convert.ToBoolean(Eval("LeadsAccess"))%>'/>

How do I conditionally display text in an ASP TemplateView?

I have a few fields that look like this in a website that uses ASP and VB (the data is displayed in a gridview):
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server"
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
And what I'm trying to do is display nothing in the field if the data is empty, and display the string you see in the Text property if there is data. Currently it displays the hyphen used in the Text string when there is no data. I tried several methods of formatting the Eval that I found online but was unable to find a working solution. I also tried using the
EmptyDataText
property however this seemed to have no effect.
I am new to ASP so that could be user error. Any help is greatly appreciated.
You can also use eval for visible and check for data
<asp:TemplateField HeaderText ="Comp" SortExpression="NAM_CMPT" ItemStyle-Width="50%" ItemStyle-Wrap ="false" ItemStyle-HorizontalAlign ="left">
<ItemTemplate>
<asp:Label ID ="Label_Comp" runat="server" visible='<%# If(String.IsNullOrEmpty(Eval("CDE_CMPT")), false, true)'
Text='<%# Eval("CDE_CMPT") + " - " + Eval("NAM_CMPT")%>' />
</ItemTemplate>
</asp:TemplateField>
I haven't used VB.net is a while, so the syntax might be off.

Not able to access Checkbox values in IE9

I have an asp.net web page where i have a checkbox control inside gridview control as under
<asp:GridView ID="grdMergeCoverage" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<input type="checkbox" id="chkSelectStream" onclick="OnSelectionChanged(this);"
testrunid='<%#DataBinder.Eval(Container.DataItem,"TestRunId") %>'
checked='<%#DataBinder.Eval(Container.DataItem,"CodeCoverageRequired") %>'
servername='<%#DataBinder.Eval(Container.DataItem,"ServerName") %>'
runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
And the OnSelectionChanged function is as under
function OnSelectionChanged(id)
{
alert(id.testrunid);
alert(id.servername);
alert(id.checked);
}
In IE8 , I am able to get the value for testrunid,servername while in IE 9, it is coming as NULL.
What is the problem and how can i get the value in all the cases?
To get custom attributes you should use the getAttribute method.
Like so: id.getAttribute("servername").
The way you used to get the value before is non-standard and not always supported.
you can add the asp checkbox and add the attribute to it on the gridview rowitembound event. it will fire....

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.

ASP.NET Event Handler for label in Datalist

I'm working on an ASP.NET page, and have a DataList with a non-visible label, ID, inside of it. The Datalist is populated by Query A.
I'm trying to add a handler in my VB code where it will run Query B, populating a different label in the Item Template, after ID is changed. I figure I need to go somewhere in the DataList, but poking through Intellisense and a google search weren't successful. Does anyone know how I get to that label? Sorry if this is a dumb question, and thanks for the help.
Edit: I see how I can access the datalist items while inside the function, but how do I do it for only one part of the DataList control in the Event Handler? All the options I'm seeing are related to full events involving the Data List, not a single label changing. Thanks.
Edit 2:
I figured I'd add some code, to better explain exactly the exact issue I'm having.
<asp:DataList ID="DataList1" runat="server" DataSourceID="Omitted,Ilikemyjob">
<ItemTemplate>
<asp:Label ID="FromLabel" runat="server" Font-Size="Small" Text='<%# Eval("IncdntDate") %>'></asp:Label><br />
<asp:Label ID="ToLabel" runat="server" Font-Size="Small" Text='<%# Eval("Roadway") %>'></asp:Label><br />
<asp:Label ID = "lblCrossroad" runat ="server" Font-Size = "Small" Text = '<%# Eval("Crossroad") %>'></asp:Label><br />
<asp:Label ID = "lblRdwyID" runat ="server" Font-Size="Small" Visible = "false" Text = '<%# Eval ("RdwyID") %>'></asp:Label>
<asp:Label ID = "DistanceLabel" runat ="server" Font-Size = "Small" Text = '<%# Eval("RptTime") %>'></asp:Label><br />
<asp:Label ID = "lblTTime" runat ="server" Font-Size = "small" visible ="false"></asp:Label>
<hr />
</ItemTemplate>
</asp:DataList><br />
There's the DataList thing I'm doing, and I have a different query that I want to store the value of to lblTTime if lblRdwyID is set to a certain range of values, thought it will not always be set to a value.
I'm trying to set up an event that will trigger when lblRdwyID is set, that will launch the other query and set the value of that templated items lblTTime and make it visible. Is there a good way to do this? I tried messing around with DataList1's events, but couldn't get anything to do the trick.
If your label has its visibility set to false, it won't render on the screen (as opposed to having a div block with visibility set to hidden), and so I wager that is why you can't actually find the control after the binding has occurred.
Have you tried using the DataKey property to store the ID instead? It is easily accessible at the row level and you can store additional data if needs be.
Assuming you have the row, you use:
row.FindControl("mylabelid")

Resources