There is no asp:HiddenField that can be used in a GridView so I was wondering what would work similar to this.
My reasoning for wanting this is I have a ButtonField that triggers an OnRowCommand. From there I can figure out which row was selected, but I cannot retrieve the text value from the ButtonField to see the data that was bound to it (through DataTextField).
My solution to this was to have a BoundField and just retrieve the text value from it instead, since I already knew which row was selected. This worked, but I need it to be hidden.
Someone suggested using a HiddenField nested within a TemplateField, but I had troubles retrieving the text value from that HiddenField. Is there some way to access the control within the TemplateField to get the text value of the HiddenField?
If anyone has any suggestions for alternatives that would be great as well.
Thanks,
Matt
You can use a template field with the commandArgument equal to the the ID of the record/row.
<ItemTemplate>
<asp:Button ID="Button1" runat="server" CausesValidation="false"
CommandName="SomeCommand" Text="Button" CommandArgument='<%# Eval("SomeID") %'></asp:LinkButton>
</ItemTemplate>
Then, in the row command handling event you can get that value by e.CommandArgument
You can use the DataKeyNames property on the Gridview.
gridView.DataKeyNames = { "values", "you", "want "};
and then you can access them like this:
gridView.DataKeys[rowId].Values["value"];
is it possible to add another ID column, as a unique representative for a record(row) ? That particular ID column can be set with style: display=none in order to hide it, but still the ID is inside the HTML form.
and then you can retrieve the value of that particular ID column to be processed further.
hope this helps,
hadi
Related
I have been searching for hours and can not find a solution. i was wondering is there a way to add sql data to a tool tip in ASPX? At the moment i have a GridView which shows information from a database. The final column is a total column, i would like the tool tip to show how the total was got.
What would be the best way to do this? I am using vb codebehind for this project.
Thanks
Make the cell of your grid view that holds the total value be a server control, like Label, and then bind the text to the ToolTip property of control like this:
<asp:Label id="LabelTotal" runat="server" ToolTip='<%# Eval("Formula") %>' />
Obviously, this means you need to have a property named "Formula" in the list of objects you are binding to your grid view or a database column named "Formula".
I am using asp.net with vb.net code behind. I have a database that contains a list of names. I use an asp:dropdownlist databound with SqlDataSourceID and it works fine to get the list from the database to the dropdownlist and display it.
However, I want to use the dropdownlist to also allow the user to add unique values to the dropdownlist and therefore edit the database.
How do I do this? I cannot enter unique values in the dropdownlist now. Below is the asp snippet.
Thanks for the help.
<asp:DropDownList ID="OEMDropDownLst" runat="server" AutoPostBack="true"
CssClass="NwDrpDwnFmt" DataSourceID="SqlDataSource1" DataValueField="CustomerName"
AppendDataBoundItems="True">
</aso:DropDownList>
If I understand correctly you want users to type directly into your dropdownlist? As far as I know that can't be done. If you want users to add new options to that dropdownlist then you have to create the fields needed and have an update button so then you rebind the dropdownlist after changes have been saved.
</aso:DropDownList></asp:DropDownList> Spelling mistake brother.
I've got a ListBox within a DetailsView, each having a different (Sql)DataSource:
<asp:DetailsView ID="dvwSomeDetailsView" DataSourceID="sdsSomeDetailsView" runat="server" ...
...
<asp:TemplateField HeaderText="SomeHeaderText">
<ItemTemplate>
<asp:ListBox ID="lstSomeListBox" runat="server" DataSourceID="sdsSomeListBox" DataTextField="SomeColumn" DataValueField="SomeOtherColumn" Rows='<%#Eval("NumberOfRows") %>' />
</ItemTemplate>
</asp:TemplateField>
....
</asp:DetailsView>
....
Trying to access the column "NumberOfRows" now seems to try and read it from the SqlDataSource that is associated with the DetailsView as an exception is thrown: "DataBinding: System.Data.DataRowView does not contain a property with the name NumberOfRows".
Filling the items of the ListBox with what is returned from the SqlDataSource for that ListBox is not a problem, but how can I access data from other columns (if at all possible)? Just entering the column name as in Rows="NumberOfRows" does of course not work.
Update
Clarification of what I would actually like to do: I want the "Rows" property of the ListBox to be set dynamically to the number of items, i.e. if there are six items, "Rows" should be set to six so no scrolling is neccessary and not empty space is in the listbox.
Thanks in advance.
G.
Not sure if this is entirely what you want, but you can try this:
// Get the ListBox - I used the first row for sake of illustration
ListBox lb = (ListBox)dvwSomeDetailsView.Rows[0].FindControl("lstSomeListBox");
// Now you can access the ListItemCollection of the ListBox
string value = lb.Items[0].Value;
Hopefully that will at least get you going in the right direction.
I have a master-detail page, in which I use GridView to display multiple rows of data, and DetailsView + jQuery dialog to display the details of a single records. only one DetailsView is open at a time.
I need to be able to pull out a single field of the open DetailsView, for manipulation using JavaScript. Is there a way to give a unique ID to a given field in DetailsView, so I can use getElementByID? Or is there another way to accomplish what I'm trying to do?
Thank you in advance.
If you are using a bound textbox in a template field in your detailsview you can then select it by:
$("[id$='MyTextBox']");
Which will find the textbox bound to MyFieldName as below.
<asp:DetailsView ID="DetailsView1" runat="server">
<Fields>
<asp:TemplateField>
<ItemTemplate>
<asp:TextBox ID="MyTextBox" Text='<%# Bind("MyFieldName")%>' runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Fields>
</asp:DetailsView>
Whatever guff asp.net adds onto the begining of the id won't matter because jQuery $= mean "ends with".
there may be a better way, but when I've needed an id available for js work, I just convert the bound field to a templated field. you can then give the textbox the id of your choice. keep in mind when rendered the id will be expanded with the id of the parent control.
I have a dropdown, I have a datasource, I have AutoPostBack set to true.
I want to add a first entry to the datasource that says something like "--- select country ---" and selecting this entry won't cause postback.
This feels like it should be easy to do, yet I can't seem to be able to find a good solution.
Thanks.
In your aspx page (the important part is handling the DataBound event and setting CausesValidation="true" to force validation of a drop down list):
<asp:DropDownList ID="ddlCountries" runat="server" DataSourceID="dsCountries" AutoPostBack="true" OnDataBound="ddlCountries_DataBound" CausesValidation="true" />
<asp:RequiredFieldValidator ID="rfvCountries" runat="server" ControlToValidate="ddlCountries" Display="Dynamic" ErrorMessage="Please select a country." />
In your codebehind (it is important that the value of the inserted item is String.Empty for the required field validator to work!):
protected void ddlCountries_DataBound(Object sender, EventArgs e)
{
ddlCountries.Items.Insert(0, new ListItem("--- select country ---", String.Empty));
}
Note: If you don't want the validator's message to display, set the "Display" property to "None".
You can also add the row manually through the designer but you have to make sure that the DropDownList's property AppendDataBoundItems = True as well so that the databound rows are tacked onto the first row.
Previous answers deal with inserting the value, but I understand your problem is the AutoPostBack property. I suppose you dont want to postback that value and that's your problem, am I right?
Maybe there's a better solution, but I'd suggest not using AutoPostBack. You could handle postback automatically using the selected value change event.
IMHO if the AutoPostBack does not work as you want, it's always better to implement your own solution that to put some kind of "patch" over it to "fix" it.
Hope that helps
Use the insert method as others have suggested to add the item at index 0 with a value to indicate not selected ( for example 'unknown'). Then use validators, add a required field validator and set the InitialValue property to the value of the new list item ('unknown' in our example).
Set index 0 to be the selected item on page load and if not postback.
If the user doesn't select another option the validator will prevent the postback.
Hope that's what you are looking for.