This will not be a very clear explanation of my problem, but I don't know how to explain it better.
I have a gridview which I create dynamically on PreInit. This gridview has textboxes dynamically added on each row.
Everytime I push the button, I loop inside the gridview cells and get the Text of the textboxes -and update the database.
the first time the gridview is created, row uniqueID's are like this:
ctl03, ctl04, ctl05, ctl06 (thus, the textbox ID's are ctl03$txt0 etc..)
The first time I push the button, the row UniqueID's are still the same, so that I can find the controls by FindControl(ID) method, or using Request.Form[txt.UniqueID]
However; after the first time, whenever I push the button, the row ClientId's are created like the following: ctl02, ctl03, ctl04, ctl05.. So that I cannot find the Textboxes and cannot catch the Text written on them.
When I look at the rendered HTML code, I see that the rowClientID's are still the same with the first created ones (ctl03, ctl04, ctl05, ctl06)
Does anyone have any idea why the rowIDs (naming container IDs) change after the first update?
Thanks in advance.
One solution is to create your textboxes with static ids and not let it dynamic create by asp.net. This can be done if you use asp.net ver 4.
Other solution is to just render a simple <input name="KnowName01" id="KnowId01" type="text" value="your value here" maxlength="100" etc... >
and then on post back you just capture the return with the old way and get the value.
Request.Form["KnowName01"]
At the end, the TextBox is nothing more than a render of the input, plus some checks for what write on it, including Anti-XSS safe.
Related
i want that after clicking a button saves the index i have selected in order to storage that number to my database. My drop-down-list is in a comment below.
The drop-down-list is filled by using a query. And so far what it does, is that after clicking the button it storages the first index (which is 0), even when i want to save another index.
I have done some research, and so far i cannot solve it, hope someone can help me out.
Code for Dropdown list:-
<asp:DropDownList ID="ddlCategory" runat="server" Height="25px" Width="428px" Font-Size="Large"> </asp:DropDownList>
here's how i fill the drop-down-list
ClsUtil clsutil = new ClsUtil();
DataSet ds = clsutilerias.FillDDL(Category);
ddlCategory.DataTextField = ds.Tables[0].Columns["Name"].ToString();
ddlCategory.DataValueField = ds.Tables[0].Columns["Category_ID"].ToString();
ddlCategory.DataSource = ds.Tables[0];
ddlCategory.DataBind();
The DropDownList has properties. You need to set SelectedIndex, SelectedItem or SelectedValue properly (and in the correct event in relation to when you do the binding etcetera).
Edit: Maybe you updated your question, or maybe I misunderstood, but either way it seems now that you don't want to set the SelectedIndexperhaps as much as get the SelectedIndex. Even so, if you read the documentation and look at the examples provided in the links, you should have enough information to know what to do.
Basically, one of the <option> elements in the <select> on the client (the HTML code) will be selected when the data is posted back to the server. If your <asp:DropDownList> is AutoPostBack="True" you can go for a callback in the OnSelectedIndexChanged event, but there are other ways too, with or without auto-post-back. (You don't really need the view state, or even the <asp:DropDownList> at all, but can trigger a POST by submitting the form data in any way that suits your needs or preferences, and then read any values your interested in server-side.)
It seems that you found your solution in checking IsPostback in the Page, and not re-populate the list if the value is true. That's good. If you don't need to data-bind every time you render the page (even after a POST), that's a viable and common solution.
Another option, of course, would be to read the posted data in any event that happens before you do the data-binding. You may also want to handle the value of SelectedIndex to re-select the correct option in your drop-down after having populating it again, if you don't like having ASP.NET doing it for you.
When you are clicking the button, the entire page is refreshing that why you got the first index all the time
The best solution is :
if(IsPostBack == false)
{
//Save your data code here
}
You have other tricky solution which is :
put the dropboxmenu inside an update panel control
While binding datasource to combo, mention DataMember and DataValue property for combo box so that selecting elements from combo you get selectedValue as DataValue.
Well i have solved my problem, my problem was that when i clicked the button, the pageLoad method executed again, and there was where i had the code to fill my drop-down-list. so i just added:
if(!IsPostBack)
{
//here goes the code to fill the Drop down list
}
Given a data set containing multiple rows, from within a .NET console application I need to generate a report on a single page for each row, sending those pages directly to the printer.
I am attempting to use Microsoft Report for this by attaching it to a data set and placing TextBoxes where I wish. Generating the report and sending it to the printer are not a problem. Unfortunately, the data only seems to be available in aggregates -- First, Sum, Last, Max, etc. I cannot latch the text box to a bare field.
Some poking around here and other sites seems to address this, but only when the data is presented in a table. One post even said without elaboration, "My mistake was using Text Boxes"
Am I using the wrong tool for what I am attempting to accomplish?
I ran into the same problem and managed to solve it. The solution seems a little convoluted to me so don't quote me on the "right" way to do this, but here is what I did:
Make sure you have a Dataset defined for your report.
Add a "Table" control to the report. This seems to be needed in order to iterate the rows in your Dataset.
Delete the header row and two of the default columns from the table so that you are left with a single row with a single column.
Expand the table to the width of your layout and make it as tall as you will need for your "free form" layout.
By default, there is a TextBox inside the table cell. Right-click the empty table cell and choose "delete" to remove that TextBox.
Drag a "Rectangle" control into the empty table cell. It seems to automatically "dock" to the width/height of the table cell.
Now you should be able to drag the fields from your DataSet (TextBoxes, etc) into the Rectangle to produce the desired layout.
Note that I am in the early stages of using this approach so I'm not sure if I am going to hit any walls... but for a basic report that uses TextBoxes and a page break after each "row" it seems to be working ok.
Or you try to use a list.
In the list you can arange textboxes (and other controls) as you want and they will be filled for each record in the recordset.
This work for me. :-)
Hi and thanks for taking the time to asnwer my question.
I have the following problem. I have a form and a button which says "add new activities".
Whenever the button is clicked I add a new set of elements, namely 2 drop down menus + text area. How can I get the values of these newly created elements in code behind since I cannot know their ids up front?
If there is something unclear about my question, please let me know.
Thanks again!
But you must be setting id's (more importantly - name attributes) of new elements using certain pattern. Use the same pattern in a loop in server-side code to get values from Request.Form. Provide a hidden input where you put the total count of items added for the server-side to know the upper bound of loop counter.
You should set the ids when you create the elements if you plan to access them again
So if you this is your text area:
var textarea = document.createElement('textarea');
You can set the id to like this:
textarea.id = "taId";
I have a gridview with empty cells. Whenever a cell is clicked I replace the contents of the cell (innerHTML property) with a string, using javascript.
I would like to save this changes on a 2d array when the index of my combobox is changed. However when I traverse the gridview during my selectedindexchanged event, none of the changes I did to the cells are visible (all the cells are empty). I guess the changes are not persistent.
How could I do this?
No, the changes are not persistent. You should do some reading about how forms on the web work--not just specific to asp.net--to get a fuller understanding. Basically, your SelectedIndexChanged event is really a POST of a form on your page. Only form values, like those in <input> or <select> fields will be sent to the server and be available to process in your C# code. So, one option would be to have a hidden input for every cell in your GridView. Another one would be to have a single hidden input which stores a string representation of a 2d array, and you would manipulate that with JavaScript every time you change the contents of a cell. Then, when you process this data in your C# code, you'll need to process the hidden inputs, not the cells of the GridView.
Using C# & asp.net
if there is no data's in the table, gridview displaying one rows (it was empty)
I want to hide the gridview empty row.
How to do this.
Assuming that you can normally delete that one visible row just check that if a field that would normally have a value is empty and the row count is 1 then delete that row.
if(String.IsNullOrEmpty(mydatagrid.Rows[0][0].ToString()) && mydatagrid.Rows.Count==1) //Check a field that would normally have a value
{
mydatagrid.Rows.RemoveAt(0);
}
Let me know if this helps/works
If you are manually data binding than you can check at that time and hide or disable the control if there is no data. If you are data binding using the design view than you should subscribe to the DataBinding or the PreRender events on the control and check then.
you can check if the datatable doesn't have any rows
use:
mydatagrid.DataSource=null;
mydatagrid.DataBind();
As the other two comments you can either check in code and set MyDataGrid.Visible to true or false to hide the entire table, or you can not bind the datasource, or you can EmptyDataTemplate option to display whatever you want when there is no data for the GridView to display normally.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.emptydatatemplate.aspx
The normal behavior for GridView is to render NOTHING if there are no data rows. (Well, almost nothing; it insists on rendering a <div> wrapper, but that'll be empty.)
If you specify an EmptyDataTemplate, however, a one-celled wrapper table is generated to contain whatever is in your template. Even if your template is empty, you'll get that wrapper table (with its one cell, empty). Sounds like the answer to your question is don't specify an EmptyDataTemplate.