Submit TextBox From Within A Repeater - c#

I want to do something simple. I have a text box in a repeater Item that will allow people to add a note to the item. My code is not working, it doesn't seem like anything is happening at all.
ASPX:
<asp:Repeater ID="rptList" runat="server" ViewStateMode="Enabled">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="NoteTextBox" runat="server"></asp:TextBox>
<asp:Button ID="SubmitNote" runat="server" Text="Button" OnClick="lnkClient_Click" CommandName="AddNote" CommandArgument='<%# Eval("UID")%>'/>
<asp:Label ID="ShowNotes" runat="server" Text='<%# getNotes(Eval("UID").ToString())%>'></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
CODEBEHIND - This is what should be executed on click. I replaced my SQL code with Response.Write:
public void lnkClient_Click(object sender, EventArgs e)
{
Button btn = (Button)(sender);
string FID = btn.CommandArgument.ToString();
string note = ((TextBox)rptList.Items[0].FindControl("NoteTextBox")).Text;
Response.Write(FID + " " + note);
}
UPDATE: Changed some settings and now the only problem I am having is that the text entered client side is not passed to the command.

Try this
protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("AddNote"))
{
string FID =e.CommandArgument.ToString();
TextBox txtNote=e.Item.FindControl("NoteTextBox") as TextBox;
string note=txtNote.Text;
Response.Write(FID + " " + note);
}
}
and in Mark up
<asp:Repeater ID="rptList" runat="server" OnItemCommand="Repeater_OnItemCommand" ViewStateMode="Enabled">

Related

How to access a child control of parent control from another child control?

I have a repeater control to show some pictures from a specified folder. Repeater has and image and a button inside to remove that image. But the problem how to reach that image name in button click event? This is my code below:
Aspx file:
<asp:Repeater ID="rptImage" runat="server">
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Text="DELETE" OnClick="btnDelete_Click" />
<asp:Image ID="image" runat="server" ImageUrl='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:Repeater>
CS file:
private void getImages(string serialNo)
{
try
{
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images/" + serialNo + "/"));
List<String> images = new List<string>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
images.Add(String.Format("~/Images/" + serialNo + "/{0}", System.IO.Path.GetFileName(item)));
}
rptImage.DataSource = images;
rptImage.DataBind();
}
catch (Exception)
{
header.Visible = true;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//string imgUrl;
//The url of image is all i need to delete it
}
Use CommandName, CommandArgument and OnCommand properties of Button.
protected void Btn_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == "BtnClick"){
e.CommandArgument.ToString();
}
}
<asp:Repeater ID="rptImage" runat="server">
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Text="DELETE" OnCommand="Btn_Click" CommandName="Btn_Click" CommandArgument="<%# Container.DataItem %>" />
<asp:Image ID="image" runat="server" ImageUrl="<%# Container.DataItem %>" />
</ItemTemplate>
</asp:Repeater>

Change backColor of Repeater Button onClick

I have Repeater buttons that i want to change their backcolor on click.
The problem is that I dont know how to identify which button is clicked and I cant reach it through the onclick function.
Just to make it clear - I want to change the button that has been clicked.
It should be like a "selected" button.
THOSE ARE THE BUTTONS
<asp:Repeater ID="rptFillButtonCategory" runat="server">
<ItemTemplate>
<asp:Button ID="FillButton" runat="server" Width="100%" OnClick="ButtonSelectionFill"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Id") %>'
Text='<%#DataBinder.Eval(Container.DataItem, "Name") + " for next version" %>'/>
</ItemTemplate>
</asp:Repeater>
This is the on click function
public void ButtonSelectionFill(object o, EventArgs e)
{
Button btn = ((Button)o);
btn.BackColor = System.Drawing.Color.Red;
DropCategory.SelectedValue = Convert.ToInt16(((Button)o).CommandArgument.ToString());
}
Thank you for the help.
This worked:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<ItemTemplate>
<asp:Button ID="Button1" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "Id") %>' OnClick="Button1_Click" />
</ItemTemplate>
</asp:Repeater>
protected void Button1_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
b.BackColor = System.Drawing.Color.Red;
}

How to update textbox onchange without postback?

Is there a way to update a textbox without using postback? I'd like to populate the txtDFS field with the value of (100 - txtStocked) after the user enters data in the txtStocked field.
The following code works, but it uses postback so the page refreshes. Is there a way to accomplish this without refreshing the page?
<asp:TextBox ID="txtStocked" runat="server" Width="75px" AutoPostBack="true" OnTextChanged="txtStocked_TextChanged"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px"></asp:TextBox>
protected void txtStocked_TextChanged(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
txtDFS.Text = txtStocked.Text;
}
Solved this by using an UpdatePanel. Here is the updated code:
<asp:ScriptManager ID="SCManager" runat="server" />
<asp:UpdatePanel ID="SCPanel" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:TextBox ID="txtStocked" runat="server" Width="75px" autopostback="true" OnTextChanged="Update_Text"></asp:TextBox>
<asp:TextBox ID="txtDFS" runat="server" Width="75px" ReadOnly="True"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
protected void Update_Text(object sender, EventArgs e)
{
int stocked = Convert.ToInt16(txtStocked.Text);
int dfs = (100 - stocked);
txtDFS.Text = dfs.ToString();
SCPanel.Update();
}

Keep value after postback updatepanel

I have a textbox which is inside a updatepanel -> detailsview. When I click insert I basically call a method to update DB. Now I'm trying to save the value typed in the textbox in the textbox so I don't lose it.
Shortly I wanna set the textbox value, with the value that is inserted.
My aspx:
<asp:UpdatePanel runat="server" ID="insert" UpdateMode="Conditional">
<ContentTemplate>
<table>
<tr>
<Fields>
<td class="style1">
<asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="500px" AutoGenerateRows="False"
DataKeyNames="strPositionId,nFolderId,tmVaRPosition" DataSourceID="ODSManualPosVaR"
OnItemInserted="DetailsView1_ItemInserted" OnItemInserting="DetailsView1_ItemInserting"
DefaultMode="Insert" SkinID="detailsviewSkin" EnableModelValidation="True">
<asp:TemplateField HeaderText="Name" SortExpression="strPositionName">
<InsertItemTemplate>
<asp:TextBox ID="strPositionName" Width="380px" MaxLength="49" runat="server" Text='<%# Bind("strPositionName") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Width="380px" Text='<%# Bind("strPositionName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</asp:DetailsView>
</td>
</Fields>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
My Page_Load:
protected new void Page_Load(object sender, EventArgs e)
{
base.Page_Load(sender, e);
strSystemId = Request["nSystemId"];
CheckPermission(strSystemId);
if (!IsPostBack || !PageHeader1.SystemId.Equals(strSystemId))
{
RefreshGrid();
DetailsView1.DataBind();
}
}
When click insert:
protected void DetailsView1_ItemInserted(object sender, DetailsViewInsertedEventArgs e)
{
UpdateDB();
//Trying to store the textbox value in a session variable
Session["Test"] = ((TextBox)DetailsView1.FindControl("strPositionName")).Text;
KeepValues();
}
KeepValues:
private void KeepValues()
{
var txtName = (TextBox)DetailsView1.FindControl("strPositionName");
var name = Session["Test"].ToString();
txtName.Text = name;
}
When I debug it stops at KeepValues and the Session variable is working. But I still can't set the textbox.text to it.
Why does this not work? Is it because of a postback? All I want is the value inside strPositionName to be stored in a variable (working) and then set as textbox.text

How to get link button text and value in datalist?

I have an ajaxtabcontainer control in my project which contains a datalist and in datalist, I have set the names as a link button. The datalist that I am binding to a source is containing Names and age. Now I want to use the age as a selected value as we use in dropdownlist selected value? what I want is on clicking the name link button I get its value inside a label? how to do it? what I have done is as follows:
in Default aspx:
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
<ajaxToolkit:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="1">
<ajaxToolkit:TabPanel runat="server" HeaderText="TabPanel1" ID="TabPanel1">
<HeaderTemplate>
Names
</HeaderTemplate>
<ContentTemplate>
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red"
RepeatLayout="Table" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</ajaxToolkit:TabContainer>
in cs code:
protected void btn_Click(object sender, EventArgs e)
{
LinkButton myButton = sender as LinkButton;
if (myButton != null)
{
Label1.Text = myButton.Text;
}
}
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int i;
int index = Convert.ToInt32(e.Item.ItemIndex);
LinkButton lnkid = (LinkButton)e.Item.FindControl("lnkpro");
Label1.Text = lnkid.Text;
i = Convert.ToInt32(lnkid.Text);
}
but they are not working. the btn_click is working but giving the only name not its id. the second datalist_itemcommand is not working? Thank you.
you did not have OnItemCommand="datalist1_ItemCommand" in the aspx page. datalist1_ItemCommand should fire after you set OnItemCommand property.
<asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal" RepeatColumns="5" ForeColor="Red" OnItemCommand="datalist1_ItemCommand"
RepeatLayout="Table" DataKeyField="age" >
<ItemTemplate>
<asp:LinkButton Text = '<%#Eval("names")%>' ID="lnkpro" runat="server" ForeColor="Red" OnClick="btn_Click"></asp:LinkButton>
</ItemTemplate>
</asp:DataList>
If you are trying to get the age field value in the datalist1_itemCommand function, you need to set DataKeyFeild= "age". And in the item command function you can do Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]) to get the value. Your item Command function will be
protected void datalist1_ItemCommand(object source, DataListCommandEventArgs e)
{
int age = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex]);
}

Categories