linkbutton oncommand issue - c#

I am using 2 linkbuttons inside seperate dataitem server controls on my asp.net web page
<asp:LinkButton ID="Item1" runat="server" CommandName="first"
OnCommand="Item1_Onclick" CommandArgument="<%# Container.DataItem %`>"
Text="<%# Container.DataItem %`>" >
</asp:LinkButton`>
and
<asp:LinkButton ID="Item2" runat="server" CommandName="second"
OnCommand="Item2_Onclick" CommandArgument="<%# Container.DataItem %`>"
Text="<%# Container.DataItem %`>" >
</asp:LinkButton`>
When I extract the command name inside c# as
e.CommandArgument.ToString().Trim();
it does give me the correct the name however the command arugument
e.CommandArgument.ToString().Trim();
for item2 is not what I expect. It is NOT that of item1, but the one that I set initially as datasource for the datalist control of item2. It does not give me the latest dataitem string value that I am expecting out of item2 linkbutton. What can be the problem? Where am I wrong?
Also, the event for item2 is fired ONLY for the first time and not after that? Is there some silly mistake that I am doing?

I got the problem. I had not included the if(!IsPostBack) as the first statement in my void Page_Load method !! That was stupid of me. Thanks anyways for all your time and ideas.

In the command argument you are not providing the property name in the data item
"<%# Container.DataItem.ProeprtyName %`>"
Say your datasource is a User object and you need the userid as the command argument it should be <%# Container.DataItem.UserID%>`

Related

I'm using a repeater to dynamically add labels to an asp.net page, but the text is stacking vertically for some reason. Anyone know why this happens?

Output
Code
<asp:Repeater ID="rptPBM" runat="server">
<ItemTemplate>
<asp:Label runat="server" ID="lblPBM" EnableViewState="false" Text='<%# Container.DataItem.ToString() %>'/>
<br />
</ItemTemplate>
</asp:Repeater>
I'm trying to add labels to a page dynamically using the above repeater, but when they are added it stacks vertically. Anyone ever encounter this before?
You are adding a line break after each label (br tag). Was that intended?
Remember that for each item in your data set it will write out that whole statement between the tags.
Maybe try writing out the html manually until you get it how you like and then put it into the item template.
Found the problem, the DataSource and DataBind() were in a loop so it was adding one letter at a time. Once I moved them out, it works fine.

Cant change text/value of textbox inside repeater

Hello and thanks for taking your time to help me.
I'm trying to change the text of a textbox thats located inside my repeater.
<asp:Repeater runat="server" ID="rpCategories">
<HeaderTemplate>
<ul id="nav_down" class="nav_down">
</HeaderTemplate>
<ItemTemplate>
<li><%# Eval("Title") %></li>
</ItemTemplate>
<FooterTemplate>
<li></li>
<li>Contact</li>
<li><a id="cart_logo"></a>
<asp:Panel runat="server" ID="pnlBasket">
<asp:textbox runat="server" id="txtTotalCount" Enabled="false" CssClass="ltTotalCount"></asp:textbox>
</asp:Panel>
</li>
</ul>
</FooterTemplate>
</asp:Repeater>
It's the asp:textbox with the id="txtTotalCount" that I want to change the text of.
Here is my C# code:
TextBox ltTotalCount = (TextBox)FindControl("lblTotalCount");
ltTotalCount.Text = "1";
But if I run the code I get this error : Object reference not set to an instance of an object.
Would be so happy if someone could tell me what I'm doing wrong.
Becuase lblTotalCount is inside a parent control - the repeater, you have to reference it through the repeater.
You should be able to just add the id of your repeater before FindControl, like this...
TextBox ltTotalCount = (TextBox)rpCategories.FindControl("lblTotalCount");
You have to specify the repeater as the parent control to look for the text box and also since it's repeater its quite possible to have more than one text box with that Id so you have to specify which repeater item to look into like so:
TextBox ltTotalCount = rpCategories.Items[0].FindControl("txtTotalCount") as TextBox;
This will return the textbox in the first row of the repeater.
And you should use the Id value not the CssClass value
The ID is probably being 'enhanced' to prevent duplicate IDs in the rendered HTML. You can verify this by looking at the html.
You can add this to the textbox: ClientIDMode="Static" to make the ID stay the same.
You are getting the error because FindControl is returning a null but you are trying to access it anyway.
[Edit] Someone pointed out that this shouldn't work. I agree, here is what does work:
Control FooterTemplate = rpCategories.Controls[rpCategories.Controls.Count - 1].Controls[0];
TextBox MyTextBox = FooterTemplate.FindControl("txtTotalCount") as TextBox;

Deleting Selected ListViewItems from the Database

I have a ListView that I populate from the database. The ListView has a Checkbox on each row which is a built-in property of ListView.
I have been trying to get the text from the ID column next to the Checkbox which I will use in deleting the respective rows from the database but it all fails. I thought I could get the ListView.CheckedIndices and use them in return to point to the SelectedText of each index.
However all fails. More specifically i wanted to use the OnItemDeleting Event of list view but the ListView I have does not seem to have such event.
Is there any way i can achieve this?
I have had this problem before. What I did was this below:
<asp:ListView ID="xyz" runat="server">
<ItemTemplate>
<asp:LinkButton runat="server" ID="blah" OnClick="blah_Click"
CommandArgument="<%# myObject.Id %>" />
</ItemTemplate>
</asp:ListView>
I got the passed id through the command argument.Then I deleted the selected item and refreshed the data for listView. I am sure there must be a cleaner way of doing it.
Let me know how it goes.

Gridview textbox on Page_load enable edit

using Visual.Web.Developer.2010.Express;
using SQL.Server.Management.Studio.2008.R2;
N00b here,
I've got the gridview to look the way I want it to (a textbox inside of the ItemTemplate). The Textbox's class has some client-sideJS that enables a save button (an asp:LinkButton set to look like a Jquery UI save icon) to become visible after the Textbox's .keypress event fires..
Now for my question..I've looked everywhere, but I can't get how to have gridview put the Sql server db content in that textbox on Page_load (one textbox + <br /> for each row). I'm only printing one collumn from the Sql server db into the Gridview.. Also, how would I bind the asp:LinkButton save button to gridview's save event? If there is a more effecient way to do this? If you have some insight for me, please give me your opinion/!
My .aspx code
<asp:TemplateField >
<ItemTemplate>
<asp:TextBox ID="TextBox1" class="hexen" runat="server" DataField="TbValue" SortExpression="TbValue">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:FluxConnectionString %>"
SelectCommand="SELECT [TbValue] FROM [InvestigateValues]">
</asp:SqlDataSource>
Thanks in advance!
Change your text box to
<asp:TextBox ID="TextBox1" class="hexen" runat="server" text='<%#Bind("TbValue")%>' />
This will enable two way databinding.
Here is an article to get you started: http://www.devx.com/DevX/Article/35058.
The grid view and SqlDataSource expose Insert, Update and Delete event/methods. These are on a row level, not grid level.
The way I would approach your problem would be to have an onclick event for your link button that iterates through the gridview, get the data from each text box and then perform the appropriate data base action in the code behind.

Dynamic textbox ID in .aspx.cs

I have a question on C#. I use the GridView ItemTemplate to add a textbox to a whole column. I add the ID to the drop down list in ItemTemplate. Therefore, the generated ID of drop down list is 'GridViewID_dropdownListID_number' in each row when I launch the project.
However, I cannot set the drop down list to .Visible = true and .Visible = false in .aspx.cs file. I try to type the 'dropdownListID' and 'GridViewID_dropdownListID_number' to 'Page_Load' function. However, it displays the error message which is under light the statement.
'The name 'GridViewID_dropdownListID_0' does not exist in the current content'
Can I set the drop down list visible to true and false in .aspx.cs?
P.S I can retrieve the row number by GridViewRow
you can use FindControl
DropdownLIst tvSeries = (DropdownLIst)tableOfTVSeries.Rows[0].Cells[2].FindControl("tvSeriesTableCategoryDropdownLIst");
Here is an example how to do this in the item template of a repeater -- this is typically how this problem is solved:
<asp:DataList Runat="server" ...>
<ItemTemplate>
<asp:Label runat="Server" Text='<%# Container.DataItem("data") %>'
Visible='<%# Container.DataItem("makevisible") %>'/>
</ItemTemplate>
</asp:DataList>

Categories

Resources