请找到图片。这是我的整个代码,我想在保存按钮单击时保存每一行。我能够从单元格0和单元格1获取值,但无法从单元格2中获取值,其中我有下拉列表选择的值。我得到默认值"忽略"而不是选定的项目。那么如何获得下拉列表的选定值?
private int currentImage = 0;
int[] images = { R.drawable.a1, R.drawable.b1, R.drawable.c1, R.drawable.d1, R.drawable.e1, R.drawable.f1 };
private int currentImage2 = 0;
int[] images2 = { R.drawable.a2, R.drawable.b2, R.drawable.c2, R.drawable.d2, R.drawable.e2, R.drawable.f2 };
private int currentImage3 = 0;
int[] images3 = { R.drawable.a3, R.drawable.b3, R.drawable.c3, R.drawable.d3, R.drawable.e3, R.drawable.f3 };
private int[] sounds = { R.raw.sound1, R.raw.sound2, R.raw.sound3, R.raw.sound4, R.raw.sound5, R.raw.sound6, };
private int mysound = 0;
private int[] sounds2 = { R.raw.sound1a, R.raw.sound2a, R.raw.sound3a,R.raw.sound4a, R.raw.sound5a, R.raw.sound6a, };
private int mysound2 = 0;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aplhascreen);
MainImage = (ImageButton) findViewById(R.id.imageButton1);
ImageLeft = (ImageButton) findViewById(R.id.imageButton2);
ImageRight = (ImageButton) findViewById(R.id.imageButton3);
nextButton = (ImageButton) findViewById(R.id.imageButton5);
ImageLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp = MediaPlayer.create(Alphascreen.this, sounds[0]);
mp.start();
mysound++;
ImageRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp = MediaPlayer.create(Alphascreen.this, sounds2[0]);
mp.start();
mysound2++;
}
});
nextButton.setOnClickListener(aButtonChangeImageListener);
}
View.OnClickListener aButtonChangeImageListener = new View.OnClickListener() {
public void onClick(View v) {
currentImage++;
currentImage = currentImage % images.length;
currentImage2++;
currentImage2 = currentImage2 % images.length;
currentImage3++;
currentImage3 = currentImage3 % images.length;
mysound++;
mysound = mysound % sounds.length;
mysound2++;
mysound2 = mysound2 % sounds2.length;
MainImage.setImageResource(images[currentImage]);
ImageLeft.setImageResource(images2[currentImage2]);
ImageRight.setImageResource(images3[currentImage3]);
}
};
});
}}
<div>
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns = "False"
OnRowDataBound = "OnRowDataBound" Width="544px">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" />
<asp:BoundField HeaderText="Name" DataField="Name" />
<asp:TemplateField HeaderText = "Mapping" >
<ItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" style="font-size: medium; font-family: Cambria" Width="300px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button2" runat="server" Text="Save"
style="font-size: medium; font-family: Cambria" BorderStyle="Groove"
height="32px" Width="116px" onclick="Save_Click" />
</div>
private void BindGrid()
{
DataTable dt = new DataTable();
connection();
cmd = new SqlCommand("Select top 0 * from F3_BC_Product_Mapping_Data", con);
con.Open();
adapter = new SqlDataAdapter(cmd);
ds = new DataSet();
adapter.Fill(ds, "mytable");
int j = 1;
dt.Columns.AddRange(new DataColumn[2] { new DataColumn("Id", typeof(int)), new DataColumn("Name", typeof(string)) });
foreach (DataColumn column in ds.Tables[0].Columns)
{
dt.Rows.Add(j,column.ColumnName);
j++;
}
GridView2.DataSource = dt;
GridView2.DataBind();
con.Close();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DropDownList ddlMap = (e.Row.FindControl("DropDownList1") as DropDownList);
DataTable dt = new DataTable();
dt = (DataTable)Session["data"];
for (int i = 0; i < dt.Columns.Count; i++)
{
ddlMap.Items.Add(dt.Columns[i].Caption.ToString());
}
ddlMap.Items.Insert(0, new ListItem("Ignore"));
}
}
答案 0 :(得分:2)
如果您获得默认值,则必须查看绑定代码。我认为您在绑定之前没有使用if(!Page.IsPostBack)
并在下拉列表中设置默认值。尝试更改OnRowDataBound
,如下所示。
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && !Page.IsPostBack)
{
DropDownList ddlMap = (e.Row.FindControl("DropDownList1") as DropDownList);
DataTable dt = new DataTable();
dt = (DataTable)Session["data"];
for (int i = 0; i < dt.Columns.Count; i++)
{
ddlMap.Items.Add(dt.Columns[i].Caption.ToString());
}
ddlMap.Items.Insert(0, new ListItem("Ignore"));
}
}